# Undefined JSON message payload

**URL:** <https://discourse.nodered.org/t/undefined-json-message-payload/40898>\
**Category:** General\
**Created:** [12 February 2021 21:24 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898 "2021-02-12T21:24:43Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:08 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/21 "2021-02-12T23:08:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:09 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/22 "2021-02-12T23:09:39Z")

</div>

The code in the function node:

```auto
msg.payload = msg.MAC_Address;
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 23:10 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/23 "2021-02-12T23:10:32Z")

</div>

that would be ` msg.payload.MAC_Address`

`msg.MAC_Address` is undefined.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:10 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/24 "2021-02-12T23:10:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:18 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/25 "2021-02-12T23:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:18 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/26 "2021-02-12T23:18:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:19 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/27 "2021-02-12T23:19:39Z")

</div>

> [@quentinP](#):
>
> I still have it when receiving from the MQTT subscribe node though

Sorry? I don't understand what you mean.

What is `it`?

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:24 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/28 "2021-02-12T23:24:48Z")

</div>

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..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f3bd4c809feccf0220a7591a6360e8ccd392f4a9.png)

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

Output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/3629a10efeae8e916b02ea58e492403184b7cd09.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 23:25 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/29 "2021-02-12T23:25:20Z")

</div>

> [@quentinP](#):
>
> ```auto
> $ 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"
> 
> ```

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

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

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:28 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/30 "2021-02-12T23:28:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:29 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/31 "2021-02-12T23:29:20Z")

</div>

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

```auto
$ 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)?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:32 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/32 "2021-02-12T23:32:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 February 2021 23:33 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/33 "2021-02-12T23:33:22Z")

</div>

Try

```auto
"{\"key\":\"value\"}"

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:35 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/34 "2021-02-12T23:35:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:35 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/35 "2021-02-12T23:35:42Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:36 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/36 "2021-02-12T23:36:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:37 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/37 "2021-02-12T23:37:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 February 2021 23:40 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/38 "2021-02-12T23:40:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quentinP](https://avatars.discourse-cdn.com/v4/letter/q/ba9def/32.png) [@quentinP](https://discourse.nodered.org/u/quentinP)\
**Post date:** [12 February 2021 23:41 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/39 "2021-02-12T23:41:28Z")

</div>

No worries at all, thanks again for helping out!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 February 2021 23:41 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/40 "2021-02-26T23:41:55Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/undefined-json-message-payload/40898.md?page=1)
