JSON question, get one value

Help extract data from JSON
I am receiving this JSON string from a Sonoff RF bridge:

{
   "Time":"2020-08-12T20:55:56",
   "RfReceived":{
      "Sync":12980,
      "Low":410,
      "High":1270,
      "Data":"DD80F2",
      "RfKey":"None"
   }
}

I just want the data. (msg.payload = DD80F2).
When I put this into a function:

var myData = msg.payload.RfReceived.Data;
msg.payload = myData;
return msg;

I get this error:
TypeError: Cannot read property 'Data' of undefined

What am I doing wrong?

Can you feed the input into a debug node and post a screenshot please?
[Edit] If the debug says it is a string then feed it into a JSON node then into your function.

Are you sure that that is all contained in msg.payload (so it would be msg.payload.RfReceived.Data or might it be outside msg.payload an be msg.RfReceived.Data?

Thanks for looking at it. This is the whole object:
{"topic":"tele/bridge1/RESULT","payload":"{\"Time\":\"2020-08-12T21:32:18\",\"RfReceived\":{\"Sync\":13080,\"Low\":420,\"High\":1270,\"Data\":\"DD80F2\",\"RfKey\":\"None\"}}","qos":0,"retain":false,"_msgid":"ae7c74e7.0c06a8"}

data

Your payload is a string. Either feed it through a JSON node or set your mqtt in node to automatically parse JSON strings. Then you can simply copy the path to the item of interest from the debug window copy path button.

Thanks- that was it.
What was the giveaway? That everything was quoted?

That & the fact it looks like JSON and it wasn't an expandable object and lastly, it was coloured as strings are in the debug node.

In future, if unsure, you can do this in a function node...

node.warn(typeof msg.payload)
return msg;

It will output "string".

1 Like

Thanks- both good tips.

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