Undefined JSON message payload

I'm trying to retrive a JSON string from a JSON object I'm recieving from my MQTT broker. However, when using the following function node:

msg.payload = msg.MAC_Address;
return msg;

I get the following error:

12/02/2021, 17:14:20[node: 18937695.b6aa59] client1/default/default/birth/0d:1e : msg.payload : undefined
undefined

The JSON object looks as follows:

{"MAC_Address": "00:0a:95:9d:68:16", "Manufacturer": "bigboiteam", "MCU": "MSP430FR4133", "Device": "RGB_Bathroom_Light"}

I've never used JSON or javascript and couldn't find anything.

Here are the debug messages:
image

and here is the flow:

To make sure we understand can you send the MQTT message to a debug node and screenshot what it shows please?

You may need to use the JSON node after the MQTT IN node.

What you are showing won't exist out of the MQTT IN node.
The MQTT IN node will have all data in the msg.payload.......

I fell for that trap a lot of times.

I've just updated the question with both images

Thanks.

But it may be nice to name the debug nodes so we can see which message is coming from where.

I've also tried that (updated the question with a screenshot), when the node is configured for "Convert betwee, JSON String & Object" or "Always convert to Javascript Object", i get the following error:

"Unexpected token M in JSON at position 1"

In the screenshot above, I have it configured as "Always convert to JSON String".

My bad, I just updated the question with named debug nodes

Ok. Yeah, that too trips me up a lot. I can't help you with that, sorry.

So you call the MQTT node BIRTH. Ok. So you are looking at the birth certificate message?

Have you set that up to be sent?
In Node-Red you click on the messages tab and enter the message there.

If it is an Arduino type device, you have to make sure that that message is written into the code.

The string the mqqt node is sending is not a correctly pharsed json string, it is missing all quotes. Do you have control of the json that the mqtt node is receiving?

Ideally if the message is expandable (so it allows you to get to the sub-sections) there is a small black grey > at the start of the message.

Like this:

Screenshot from 2021-02-13 09-42-36

And when you click on it, it expands to this:

Screenshot from 2021-02-13 09-42-40

Of course this is an example and the actual contents will vary.

But sorry, I can't help you with why the structure isn't correct.
Above my skill set :frowning:

parsed or structured?

Not to be pedantic, but .......

I do have control over the JSON sent throught the broker to Node-RED. However, because we are developping in parrallel I am testing the Node-RED by sending direcly from my terminal on the machine hosting the broker.

When you say missing the quotes, which ones are you reffering to? (because I already have some as shown in the question in the object)

No worries, thanks for trying!

The debug image shows the structure of the string received. it has no quotes.

This one does and is correct. Maybe some one forgot the quotes when setting up the payload message.

1 Like

But @E1cid is probably right.

The structure is wrong.

What you could do is get an inject node and enter the message there - that which is being sent form the device - and put that through a JSON node then a debug node and set the debug node to show what you want.

Then, you tweak the message being injected until you see the right message.

Having done that, make the code on the remote device send that message.

Ok, he beat me to it.

Put that message that @E1cid just showed you into an inject node (parallel with the MQTT IN node.

Press the inject node and see what you get then.

Didn't realize that... but that's weird because I am including the quotes from the broker... Or is this not the correct way to do it?

$ mosquitto_pub -h localhost -m "{"MAC_Address": "00:00:00:00:00:00", "Manufacturer": "bigboiteam", "MCU": "MSP430FR4133", "Device": "RGB
_Bathroom_Light"}" -t client1/default/default/birth/0d:1e -u "user" -P "password"

Try that with single quote aroung the json string

'{"MAC_Address": "00:0a:95:9d:68:16", "Manufacturer": "bigboiteam", "MCU": "MSP430FR4133", "Device": "RGB_Bathroom_Light"}'

[edit]
you could also send it with an inject node and a mqtt out.

Ok, copy the text and paste the text rather than screen shots.

Not many can read it and it doesn't help. (well....)

Before you paste it though:

Click the </> button, press enter, paste, press enter and press the button again.

That way we can all see/read it.

Ok, so I now get the dropdown that you mentioned but the same undefined message for the debug node trying to extract the MAC_Address... Is my javascript wrong?

image

So whats in the function node?
did you return the msg or over write payload?