Payload Parser error

Good mornin.

I would like to inform you about an issue I am experiencing with the payload, which is saving the data to the bucket separately. That is, the variable, the value and the unit are being saved in different variables.

I was wondering if you could help me create a payload parser on the Device, so this doesn’t happen and we can access the variable with its value and unit together. I would greatly appreciate your assistance in this matter.

I look forward to your prompt response.

Hi Octavio,

I believe 30 is tying the variables together and V is for Value, U for Unit and N for name? In this case you could use the following parser:

function ParsePayload(input_payload) {

// Create a dictionary to hold the combined data

const combinedData = {};

// Iterate over each item in the input payload

input_payload.forEach(item => {

// Extract the group number and type (n, u, v) from the variable name

const [group, type] = item.variable.split(‘_’);

// Initialize the group in the dictionary if it doesn’t exist

if (!combinedData[group]) {

combinedData[group] = { group: item.group };

}

// Depending on the type, assign the value to the correct property

if (type === ‘n’) {

combinedData[group].variable = item.value;

} else if (type === ‘u’) {

combinedData[group].unit = item.value;

} else if (type === ‘v’) {

combinedData[group].value = item.value.toString(); // Convert to string to match expected output

}

});

// Convert the dictionary to an array of the combined data objects

const output_payload = Object.values(combinedData);

return output_payload;

}

// Call the ParsePayload function with the example input

payload = ParsePayload(payload);

Hope this helps! :slight_smile: