Extract values from a json object using a function node

16,660 views
Skip to first unread message

Luis GCU

unread,
Apr 26, 2017, 10:02:40 PM4/26/17
to Node-RED
Hello gents 
I  am trying  to extract values from an json object using a function node, tried to many ways and nothing seems to work fro me.
the example below I would like to have function node with 5 outputs for the numeric values.


VFD_ID1Humid34Temp-f77DewP-C8.04HeatI-C25.51 }



this is the complete  message object 

rawobject
data"{"VFD_ID":1, "Humid":34.00, "Temp-f": 77.00, "DewP-C":8.04, "HeatI-C":25.51 }"
ttl"60"
published_at"2017-04-27T01:58:12.046Z"
coreid"2d0035001647343337363432"
evtname"ENVIROMENT-DATA"
payloadobject
VFD_ID1
Humid34
Temp-f77
DewP-C8.04
HeatI-C25.51
published_at"2017-04-27T01:58:12.046Z"
id"2d0035001647343337363432"
_msgid"3b2d798c.c4d286"

thanks you

Nate Damm

unread,
Apr 26, 2017, 11:12:54 PM4/26/17
to Node-RED
I did something similar with a split and switch node for my bme280 sensor, but it spits out less info than this I believe.

first I used a split node with  ,
then a switch looking for each part, directed to individual outputs

Here's what debug shows me straight from that sensor,

"{"sensor":"bme280", "humidity":32.27, "pressure":939.14, "temperature":18.55, "altitude":634.05, "timestamp":1493262621}↵"

and heres how I did it,

[{"id":"78a17317.eda3ac","type":"split","z":"111d7925.30cd37","name":"","splt":",","x":230,"y":240,"wires":[["d64d3839.9c52d8"]]},{"id":"d64d3839.9c52d8","type":"switch","z":"111d7925.30cd37","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"\"humidity\":","vt":"str"},{"t":"cont","v":"\"pressure\":","vt":"str"},{"t":"cont","v":"\"altitude\":","vt":"str"},{"t":"cont","v":"\"temperature\":","vt":"str"}],"checkall":"true","outputs":4,"x":370,"y":200,"wires":[["c404b2e7.e4f15"],["fdf4dd2e.87b1"],["6b4f5b74.387c34"],["ca504f24.b00b2"]]}]


steve rickus

unread,
Apr 26, 2017, 11:45:30 PM4/26/17
to Node-RED
Hey Luis,

Let me see if I understand what you want -- the payload shown below should be split into individual msg objects? What do you want each msg to look like?

The node-red core 'split' node will break you payload into 5 msgs, each with a payload of the value, and a parts field with the key name and other internal information, like this:

4/26/2017, 11:23:10 PMnode: b9221c0b.21fe4msg : Object
object
_msgid"d419232b.68c6f"
topic""
payload34
partsobject
id"d419232b.68c6f"
type"object"
key"Humid"
index1
count5
The internal fields are used by the 'join' node to put them back together later (if you need to).

But, if you want to create individual msgs with a topic (the key) and a payload (the value), say for pushing to MQTT listeners, you can write a function node, something like this:

var keys = Object.keys(msg.payload);

var msgs = keys.map(function(key) {
   
return { topic: key, payload: msg.payload[key] };
});
return [msgs];

If the number of output ports on your function node is 1, all 5 msgs will be sent down the same wire -- you can also set your output to 5 ports and have one msg sent to each port. Unfortunately, the order of the keys returned will not always be the same, so you can't count on a given topic always being sent to the same port number. So probably not a very robust solution, unless you write the code to build the output msgs array in a particular order.

Nate mentioned some of the struggles he had parsing his data string into keys and values. I can see that your payload is already an object, but in his case, I would pass the text to a 'json' node, which parses the text and builds the object for you -- much easier to manipulate in that form. And if you haven't seen it yet, I recommend you use the 'change' node with a JSONata expression if you need to restructure more complicated payload objects. Have fun!
--
Steve

Glenn

unread,
Apr 27, 2017, 12:13:59 AM4/27/17
to Node-RED
Does this page help

eg msg1 = evtname.payload.VF_ID

Luis GCU

unread,
Apr 27, 2017, 4:30:16 PM4/27/17
to Node-RED
Hello guys
I have to try all your suggestion when I get to home today..
basically what I need is to display the value of each variable in dashboard , I did that using change node, I was trying to do the same using a function node instead of the change node ..
it luck ugly have change node  to extract the value of each variable .
See the capture attached.

regards, 

steve rickus

unread,
Apr 27, 2017, 5:32:48 PM4/27/17
to Node-RED
Instead of 5 change nodes, you could use "json" -> "split" -> "switch" (with 5 tests and 5 outputs - well maybe 6 to catch anything else)

The switch node can (for instance) test for msg.payload.parts.key == "Humid" and route the value to port #1, which is wired to the correct guage or chart.
Reply all
Reply to author
Forward
0 new messages