How to compose a array in required format

Hi,

I am trying to format and push an array to a Powerbi Streaming dataset using HTTP request node.

Below is my code in the function node where data arrives from LORAWAN and after this I directly connect it to HTTP node with a POST Powerbi streaming dataset URL and debug node :

var payload = msg.payload;

if (payload.deviceName == "co2")

{
msg.topic = "co2";
msg.payload = payload.object.co2;
    
}
return msg;

I require the output in the below array format as Powerbi requires it in this specific format. How do I format it using node-red?

[
{
"co2" : 98.6
}
]

I had previously done a similar task using PowerAutomate and I had used compose step.

My flow looks like below -

Please let me know if you need more information.

Kind regards,
Amit

Only a guess since you have not shown us the data that you receive from Lorawan, but would this work?

msg.payload = [{ "co2": msg.payload.object.co2 }];
return msg

The data I receive from lorawan is as given below -

{"topic":"application/1/device/a81758fffe070338/event/up","payload":{"applicationID":"1","applicationName":"default-app","deviceName":"co2","deviceProfileName":"ELSYS-OTAA","deviceProfileID":"632f3e15-d21b-417a-b337-6077fa362295","devEUI":"a81758fffe070338","rxInfo":[{"gatewayID":"0000000000000000","uplinkID":"f02b8781-c5d5-4175-8097-f3d4756b78ff","name":"default-gateway","rssi":-22,"loRaSNR":9,"location":{"latitude":0,"longitude":0,"altitude":0}}],"txInfo":{"frequency":916600000,"dr":0},"adr":true,"fCnt":13022,"fPort":5,"data":"AQDxAikEAGUFAAYB1wcOJw==","object":{"co2":471,"humidity":41,"light":101,"motion":0,"temperature":24.1,"vdd":3623}},"qos":0,"retain":false,"_msgid":"034b098c3516a9a9"}

From these I am only pulling CO2 for now to push to powerbi streaming dataset.

I added a function node between CO2 and http node and added the code you have suggested.

msg.payload = [{ "co2": msg.payload.object.co2 }];
return msg

Debug returned below response -

TypeError: Cannot read property 'co2' of undefined

Where do I tweak this so I get format like below to push as a body for POST powerbi streaming dataset URL.

[
{
"co2" : 98.6
}
]

Kind regards,
Amit

It is undefined - ie. it cannot find "co2".
So are you sure that the payload is actually arriving into the function node ?

Look at the lorawan data in the debug pane.

Where there is a small triangle next to an element name you can click it to drill down into the data.
Find the co2 reading that you want to extract and click the button at the right "Copy path".
Use that path in your function.

I notice that your data is badly formatted - msg.payload has another "payload" within it.
So the data you want could be eg msg.payload.payload.rxInfo[0].loRaSNR and in that case your function would be

msg.payload = [{ "co2": msg.payload.payload.rxInfo[0].loRaSNR }];
return msg

Obviously you will have to find the real element path.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.