简体   繁体   中英

How do I extract the value of 'temp' from a nested JSON payload in Node-red?

{"rssi":-46,"message":"{\"temp\":55,\"hum\":88,\"moist\":50}"}

In the above payload, I can extract the value of rssi using the following code in Node-red.

p = JSON.parse(msg.payload);
node.log(typeof p);
msg.payload = p.rssi;
return msg;

But how do I extract the value of temp from this.

There is a colon missing in the source json. It should be

{"rssi":-46,"snr":{"temp":55,"hum":88,"moist":55}"}

Like @Andreas mentioned, after first line its all about accessing the object "p".

You should be able to access the temp using

msg.payload=p.snr.temp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM