Extract values from complex payload

I'm a relative newbie to Node-Red, but capable developer in some other languages. What would be the best way to approach extracting the Temprature and Humidity values from the following payload?

The payload is being recieved over MQTT with AEDIS.

{"app_id":"testlorawandraginonode","dev_id":"mydevid,"hardware_serial":"00A1B2C3D4E5C6","port":1,"counter":21,"payload_raw":"EnCrYpT3d","payload_fields":{"Humidity":25,"Temperature":23.4},"metadata":{"time":"2020-08-22T06:15:33.41123456789Z","frequency":923.4,"modulation":"LORA","data_rate":"SF7BW125","airtime":56576000,"coding_rate":"4/5","gateways":[{"gtw_id":"eui-00800000a01234c6","timestamp":2370682735,"time":"2020-08-22T06:15:33.123456Z","channel":1,"rssi":-119,"snr":-1.2,"rf_chain":0,"latitude":-27.31055,"longitude":152.99,"altitude":15}]}}

Hi, firstly, I edited your post to correctly format code (it was being corrupted by forum formatting as you used "quote" button not the "code" </> button :slight_smile:

Not to be too presumptuous but I guess you are not familiar with JSON?

So a small lesson on JS objects and their equivalent string representation (AKA JSON)...

Your payload is a string/JSON (due to the Output setting on the MQTT node). You could attempt some complex splits and loops etc - but - as it is JSON (JavaScript Object Notation) - just let JS do its thing - turn that JSON back into a JS Object! (if you dont know what I mean, it'd be worth reading up - will help you greatly in node-red knowing the difference)

So first off, change your MQTT IN to...
image
... once you have turned the JSON back into an object, you will be able to simply "copy path" to any item in the object on the debug sidebar
P4lNSokZZp

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

https://nodered.org/docs/user-guide/messages

HOWEVER...

The data you posted is not quite valid JSON (missing " after "dev_id":"mydevid)

Do you have control over what is generating this bad JSON? If so, i recommend fixing this at the src - it will make your life soo much easier. The alternative is probably a regex or string parsing (not pretty and not necessary if JSON is correctly formatted)

or perhaps this was simply a copy/paste error?


Hope that helps.

1 Like

@Steve-Mcl,

That is hands down the best reply I have ever had to a question I've posted. Thank you for being excellent!.

Showing the JSON path is actually a really great feature too.

I'll keep plugging away. I think that's exactly what I needed to get going. Thank you.

Haha - excellent response :smiley: .

You're welcome.

Happy learning :+1:

1 Like

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