# How to compose a array in required format

**URL:** <https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895>\
**Category:** General\
**Tags:** http-request, function-node\
**Created:** [9 December 2022 01:17 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895 "2022-12-09T01:17:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)\
**Post date:** [9 December 2022 01:17 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/1 "2022-12-09T01:17:13Z")

</div>

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 :

```auto
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?

```auto
[
{
"co2" : 98.6
}
]

```

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

 ![Screen Shot 2022-12-09 at 12.12.21 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f894da97825db2a4c289df8df1fb922c9530fa2.png)

My flow looks like below -

 ![Screen Shot 2022-12-09 at 12.15.35 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab16580260cf7e5894c3ad13dd818ddf04dd3522.png)

Please let me know if you need more information.

Kind regards,  
Amit

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 December 2022 02:03 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/2 "2022-12-09T02:03:38Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)\
**Post date:** [9 December 2022 02:58 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/3 "2022-12-09T02:58:04Z")

</div>

The data I receive from lorawan is as given below -

```auto
{"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.

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

```

Debug returned below response -

```auto
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.

```auto
[
{
"co2" : 98.6
}
]

```

Kind regards,  
Amit

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 December 2022 05:22 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/4 "2022-12-09T05:22:32Z")

</div>

> [@amit\_meld](#):
>
> `TypeError: Cannot read property 'co2' of undefined`

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 December 2022 06:14 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/5 "2022-12-09T06:14:21Z")

</div>

Look at the lorawan data in the debug pane.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abd935020d6c7d1d5c76648a524c7a69dacad48e.jpeg)

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

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

```

Obviously you will have to find the real element path.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 February 2023 06:15 UTC](https://discourse.nodered.org/t/how-to-compose-a-array-in-required-format/71895/6 "2023-02-07T06:15:03Z")

</div>

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