# Need help in Nested JSON object

**URL:** <https://discourse.nodered.org/t/need-help-in-nested-json-object/75010>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 February 2023 20:16 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010 "2023-02-10T20:16:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)\
**Post date:** [10 February 2023 20:16 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/1 "2023-02-10T20:16:03Z")

</div>

I am trying to collect sensor data from ESP32 to RPi.  
I am getting a packet of data containing strings in intervals of 10secs(this can be changed). I am getting sensor data, along with geolocation and device ID which comes in a single packet. I want to append the timestamp and convert the whole packets into JSON format and send it via MQTT to AWS. Can someone help me with how to do this?

Thank you

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 February 2023 21:00 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/2 "2023-02-10T21:00:54Z")

</div>

Yes we can help you but it’s unclear (at least to me) if the data getting into Node-RED is all arriving at the same time in one msg or does the data arrive at various times.

If it is in one msg and you just want to add a timestamp, you might be able do it in a `function` node or a `template` node or maybe even with a `change` node.

Show us the data you are getting and an example of what it should look like to send off.

---

<div class="post-metadata">

**Author:** ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)\
**Post date:** [10 February 2023 21:08 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/3 "2023-02-10T21:08:27Z")

</div>

This is my incoming data  
"geodata=15.34-34.67;device\_ID=3;Temperature=10 degC;Humidity=2 RH;WindSpeed=5 m/s"

and I would like to send it in this format  
{"geodata": " ",  
"device\_ID: ,  
"value": {  
"timestamp": , // this is an array  
"Temperature": , // this is an array  
"Humidity": , // this is an array  
"Windspeed": // this is an array  
}  
}  
Here the data inside the timestamp, temperature, humidity, and windspeed will be an array because I will be sending the data to the cloud occasionally. For example, If I am getting data from ESP32 every 10secs, then I will append the data for 2mins, and send the complete set of data at once every 2mins. I hope this is clear.

---

<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:** [10 February 2023 21:25 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/4 "2023-02-10T21:25:36Z")

</div>

You could try something like this in a function node (assuming it always arrives in this order):

```auto
const i = msg.payload.split(";")

const geodata = i[0].split("=")[1];
const device_ID = Number(i[1].split("=")[1]);
const Temperature = i[2].split("=")[1];
const Humidity = i[3].split("=")[1];
const WindSpeed = i[4].split("=")[1];

msg.payload = {
    geodata,
    device_ID,
    values:{
        Temperature: [Temperature],
        Humidity: [Humidity],
        WindSpeed: [WindSpeed]
    }
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)\
**Post date:** [10 February 2023 21:35 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/5 "2023-02-10T21:35:32Z")

</div>

I had already tried this, but my requirement is to send the appended temperature, humidity, and windspeed data. If I get 10 data packets from esp32 and I want to send an array of data. I need to have geodata and device\_ID as one entry but temp, humidity, and windspeed can have multiple entries like an array.

---

<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:** [10 February 2023 21:40 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/6 "2023-02-10T21:40:04Z")

</div>

Are geodata and device\_ID always the same?

---

<div class="post-metadata">

**Author:** ![dhanushmh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dhanushmh/32/75899_2.png) [@dhanushmh](https://discourse.nodered.org/u/dhanushmh)\
**Post date:** [10 February 2023 21:45 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/7 "2023-02-10T21:45:21Z")

</div>

Right now, I am testing one scenario only. So, geodata and device\_ID will be the same for now. But there might be a scenario I might have 2 or 3. Not more than that. Geodata is a unique identifier according to device\_ID.

---

<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:** [10 February 2023 21:54 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/8 "2023-02-10T21:54:13Z")

</div>

This might work but only for constant geodata.  
It constructs a compound object from successive messages, but it always returns the message as it is so far, which you may not want.

```auto
const i = msg.payload.split(";")

const geodata = i[0].split("=")[1];
const device_ID = Number(i[1].split("=")[1]);
const Temperature = i[2].split("=")[1];
const Humidity = i[3].split("=")[1];
const WindSpeed = i[4].split("=")[1];
const now = new Date()

let mydata = context.get("mydata") ?? {}
if (mydata.hasOwnProperty("geodata")) {

}
else {
    mydata.geodata = geodata
    mydata.deviceid = device_ID
    mydata.timestamp = []
    mydata.temperature=[]
    mydata.humidity=[]
    mydata.windspeed=[]
}
mydata.timestamp.push(now)
mydata.temperature.push(Temperature)
mydata.humidity.push(Humidity)
mydata.windspeed.push(WindSpeed)

context.set("mydata", mydata)
msg.payload = mydata
return msg;

```

---

<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:** [10 February 2023 22:23 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/9 "2023-02-10T22:23:48Z")

</div>

Why don't you send the complete json from the esp ?

---

<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:** [11 April 2023 22:24 UTC](https://discourse.nodered.org/t/need-help-in-nested-json-object/75010/10 "2023-04-11T22:24:35Z")

</div>

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