JSON question, get one value

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


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:


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.