node-RED json object - how to extract json values

Jéan picture Jéan · Dec 7, 2016 · Viewed 16.1k times · Source

In node-RED, I used 'batcher' node to bring 2 Humidity readings together in a msg, which I want in a json format, to use it downstream, one for inside, there other in a quad.

The constructed msg object now looks like this:

{ "topic": "Hum", "payload": [ { "inside": 29 }, { "quad": 54 } ] }

I am trying to get to elements in this object, with:

msg.payload.inside

But it is not working, how can I get to for instance the inside humidity ?

Answer

knolleary picture knolleary · Dec 7, 2016

The payload property of that object is an Array that contains two elements.

If you want to access the value of an object inside that array, you need to index into it.

Arrays are indexed from 0 - so you want to use msg.payload[0].inside

Note that this is standard JavaScript and not specific to Node-RED.