Basic advice needed on parsing JSON

#1

hi. I'm relativly new to NodeRed. I like what i know it can do but i'm just trying to wrap my head around it.

My current basic goal is to parse my Json correctly so i can enter it into an InfluxDB to read. I've got a SONOFF that i get power/energy readings from and want to add to a database. I've got the db connection working.

I can take my payload, but i'm struggling to split it down to just the ENERGY values, not the time aspect - as influx will stamp time when it inserts anyway. I belive i need to use a change node but i cant seem to get it working when i change payload to payload.ENERGY.Total for example.

JSON data from SonOFF
object
topic: "tele/SonoffPOW1/SENSOR"
payload: "{"Time":"2019-04-14T16:02:10","ENERGY":{"TotalStartTime":"2018-11-10T23:29:04","Total":53.260,"Yesterday":2.762,"Today":0.669,"Power":246,"ApparentPower":281,"ReactivePower":137,"Factor":0.87,"Voltage":245,"Current":1.149}}"
qos: 0
retain: false
_topic: "tele/SonoffPOW1/SENSOR"
_msgid: "7e33adbb.05cac4"

any help would be appreciated. sorry if i've posted in the wrong place. I did a lookup on the forum but couldnt find any results that i could use that quite matched my issue.

cheers Andrew

0 Likes

#2

Take a look at the page in the docs entitled “working with messages” https://nodered.org/docs/user-guide/messages which shows how to use the debug node to identify specific bits in the msg.

The cookbook also has some flows that might help... http://cookbook.nodered.org

But realistically to get the most from Node-RED you need to know about javascript objects and arrays and reading through one of the tutorials online is time well spent. One that often gets recommended here is the w3schools one a quick google will find it

1 Like

#3

The first thing to do is feed it through a JSON node, which will convert it from JSON (a string) into a javascript object. Then you can simply refer to, for example, msg.payload.TotalStartTime.

0 Likes

#4

This thread has a lot of discussion on just what you're trying to do. Hopefully it can give you some avenues to play with. If anything gives you trouble, get back to us in this thread and we should be able to provide further insight.

0 Likes