# 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:** 1

<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 21:24 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/1 "2021-02-12T21:24:44Z")

</div>

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:

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/479047013403b938ed7a43dbd59d4cdda46b225f.png)

and here is the flow:

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 February 2021 22:00 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/2 "2021-02-12T22:00:07Z")

</div>

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

---

<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 22:24 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/3 "2021-02-12T22:24:50Z")

</div>

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.

---

<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 22:33 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/4 "2021-02-12T22:33:43Z")

</div>

I've just updated the question with both images

---

<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 22:35 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/5 "2021-02-12T22:35:49Z")

</div>

Thanks.

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

---

<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 22:38 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/6 "2021-02-12T22:38:01Z")

</div>

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

---

<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 22:39 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/7 "2021-02-12T22:39:54Z")

</div>

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

---

<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 22:40 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/8 "2021-02-12T22:40:05Z")

</div>

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.

---

<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 22:43 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/9 "2021-02-12T22:43:14Z")

</div>

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?

---

<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 22:45 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/10 "2021-02-12T22:45:18Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac13bb86f6be6ffa0d0b4b59177ba7688bf4bba2.png)

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

![Screenshot from 2021-02-13 09-42-40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e2a44f36e3c7e6f5234579bc70fa5cb9222b8232.png)

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_☹

---

<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 22:46 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/11 "2021-02-12T22:46:23Z")

</div>

**parsed** or **structured**?

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

---

<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 22:51 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/12 "2021-02-12T22:51:17Z")

</div>

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)

---

<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 22:51 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/13 "2021-02-12T22:51:33Z")

</div>

No worries, thanks for trying!

---

<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 22:53 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/14 "2021-02-12T22:53:37Z")

</div>

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

> [@quentinP](#):
>
> {"MAC\_Address": "00:0a:95:9d:68:16", "Manufacturer": "bigboiteam", "MCU": "MSP430FR4133", "Device": "RGB\_Bathroom\_Light"}

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

---

<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 22:54 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/15 "2021-02-12T22:54:17Z")

</div>

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.

---

<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:00 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/16 "2021-02-12T23:00:53Z")

</div>

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?

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

```

---

<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:03 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/17 "2021-02-12T23:03:30Z")

</div>

Try that with single quote aroung the json string

```auto
'{"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.

---

<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:04 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/18 "2021-02-12T23:04:00Z")

</div>

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.

---

<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:05 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/19 "2021-02-12T23:05:53Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b06f59d760a31465d39993bea87c8b4764f7d356.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:07 UTC](https://discourse.nodered.org/t/undefined-json-message-payload/40898/20 "2021-02-12T23:07:05Z")

</div>

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

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