Undefined JSON message payload

No, the Javascript is right. Just you may be looking for a part of the message which doesn't exist.

What is the code in the function node?

Yeah, ok. Open the node, copy the code and paste it.

While you are at it, what does the JSON debug node show you?
And what is it displaying? The complete message or a selected part?

The code in the function node:

msg.payload = msg.MAC_Address;
return msg;

that would be msg.payload.MAC_Address

msg.MAC_Address is undefined.

That is good, but it is also bad.

Now, try this:

Copy that node and put it between the JSON and DEBUG node.

Set the debug node to show only the msg.payload and give that a go.

Forgetting the exact syntax (I've used your original code):

$ 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"

That is sending a message to MQTT.
This message only has two parts:
payload ad topic. Ok, user, etc.

But there are only 2 usable parts received.

The MQTT IN node gives you the topic and the payload.

So you need to use the JSON node to make things more as you expect to see the message.

It isn't really crucial and you could use the raw format (ie: msg.payload.MAC_Address) but it isn't the normal way.

Omg so sorry, you're right! I completely forgot the ".payload"!! The error is gone when I input the JSON object with the Inject node now, thanks! I still have it when receiving from the MQTT subscribe node though

Sorry? I don't understand what you mean.

What is it?

So when I inject the JSON payload into the function to retrieve the MAC_Address, I get the correct output on the debug console. However, when I send the JSON object from the MQTT node, I get undefined in the debug console..

This is when I publish the JSON object from the broker using the same command as mentioned before

Output:
image

the quotes are off, they either need escaping or use single quotes to wrap the json string

localhost -m '{"MAC_Address": "00:00:00:00:00:00", "Manufacture":"blah"}'

I can't say which way is better. The way you show with:
msg.payload = msg.payload.MAC_Address

Or using a JSON node.

I am still unsure myself.

I would suggest you use the JSON node only because it reduces the message back to msg.MAC_Address (and/or what ever else you sent)
and that is what you should call it at the other end of MQTT.

Doing it the way you show, you are leaving yourself open to forgetting to not calling a part of the message with the correct name. (or leaving out the .payload part of it.)

That can cause hours of grief and it is not promoting a good way of doing things.

The JSON node fixes that easy peasy.

I tried single quotes too and get the same message on the debug console

$ 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"

Could it be because of my keyboard as I use a french one (seems unlikely but I am a bit lost)?

Try the JSON node rather than the function node.

Display the ENTIRE message in the debug node.

Oh! Thought: Just to check:

Also paste what you are injecting (copy from the inject node)
I think I see another trap/mistake.

Try

"{\"key\":\"value\"}"

I'm thinking the inject node would be sending the message as expected AFTER the JSON node.
And so if he says msg.payload = msg.payload.MAC_Address it isn't going to work.

I am so confused... I tried that multiple times and just got it to work... Thanks so much for both your help!!! (I'm still trying to wrap my head around all this)

Ok, I'd better back out.

It is not fun/fair having two people helping you.

I think @E1cid may be more qualified to help you than me.

Actually, I was trying to use the JSON node using the double quotes. But by using the JSON node to convert the entire payload and using single quotes, it worked just fine! I can only mark one answer as the solution but both of you helped me out so thank you so much!

Apologies from me. I kinda goofed with the use the inject node part, because that was giving you the expected message after the JSON node.

So when you put it in parallel with the MQTT IN as I said, I was misdirecting you. Sorry.

That was a bad mistake on my part.

But I am glad you got it working. I hope my stuff up didn't cloud things for you.

Again: sorry for that.

No worries at all, thanks again for helping out!

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