# How best to extract data from 'nested' objects (or flatten the message)?

**URL:** <https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362>\
**Category:** General\
**Created:** [13 October 2024 15:51 UTC](https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362 "2024-10-13T15:51:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![impvan](https://avatars.discourse-cdn.com/v4/letter/i/8797f3/32.png) [@impvan](https://discourse.nodered.org/u/impvan)\
**Post date:** [13 October 2024 15:51 UTC](https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362/1 "2024-10-13T15:51:35Z")

</div>

I have an incoming MQTT message payload which looks like this:

"payload":  
{"Time":"2024-10-13T16:43:17",  
"DS18B20-1":{"Id":"030197946C6B","Temperature":36.8},  
"DS18B20-2":{"Id":"0302979458D1","Temperature":44.7},  
"DS18B20-3":{"Id":"030E97942425","Temperature":34.2},"TempUnit":"C"}

I wish to extract each 'Id' and its associated 'Temperature' - the Id checks against a table in GlobalContext, which swaps 'Id' for some human-readable description, and applies a calibration adjustment to the 'Temperature' value.

The function I'm using is below - but it fails because it doesn't 'drill through' each "DS18B20-x" heading. Do I /really/ need to write a separate function for each sub-object?

```auto
const sensorId=msg.payload.Id
const rawTemp=msg.payload.Temperature
const metadata=global.get('SensorTable')

if (metadata[sensorId]){
    msg.topic=metadata[sensorId].SensorLoc
    msg.payload = (rawTemp + metadata[sensorId].apply_offset).toFixed(1)
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [13 October 2024 16:07 UTC](https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362/2 "2024-10-13T16:07:05Z")

</div>

Something like this might work  
it will extract each reading, providing its an object  
(but you should check, its the object you need - just a bit of sanity checking)

```auto
for (const [key, value] of Object.entries(msg.payload)) {
    if (typeof value === 'object') {
        const tempReading = {
            device: key, /* DS18B20-1 */
            id: value.Id, /* 030197946C6B */
            temp: value.Temperature /* 36.8 */
        }
        // store/match/use tempReading somewhere (it will repeat for each object)
       // example : tempReading.id
    }
}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 October 2024 16:46 UTC](https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362/3 "2024-10-13T16:46:30Z")

</div>

I would also add, for good coding habit please pick a variable naming convention.  
**Underscores:** date\_of\_birth  
**PascalCase:** DateOfBirth  
**camelCase:** dateOfBirth  
etc  
Please try not to mix 3 different types in a piece of code it makes it unpleasant to edit, as you can not remember what each vars convention should be.

---

<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 January 2025 16:47 UTC](https://discourse.nodered.org/t/how-best-to-extract-data-from-nested-objects-or-flatten-the-message/92362/4 "2025-01-11T16:47:19Z")

</div>

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