# I am missing something here with message format - JSON

**URL:** <https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814>\
**Category:** General\
**Created:** [7 December 2021 07:33 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814 "2021-12-07T07:33:16Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [7 December 2021 07:33 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/1 "2021-12-07T07:33:16Z")

</div>

These are the 3 nodes:

```auto
[{"id":"a2b9b7d51d629336","type":"mqtt in","z":"d65ab744.465758","name":"","topic":"ARLEC-1/#","qos":"2","datatype":"auto","broker":"378c0403.8cda04","nl":false,"rap":true,"rh":0,"inputs":0,"x":810,"y":420,"wires":[["e526db181bab789a","28868a5dfe0ca601"]]},{"id":"e526db181bab789a","type":"json","z":"d65ab744.465758","name":"","property":"payload","action":"","pretty":false,"x":990,"y":420,"wires":[["abec26cc161f6cbd"]]},{"id":"28868a5dfe0ca601","type":"debug","z":"d65ab744.465758","name":"RAW ARLEC-1 message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":470,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","name":"TIMEPI MQTT","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

This is what I see as the _RAW_ message:

`{"topic":"ARLEC-1/tele/LWT","payload":"Online","qos":0,"retain":false,"_msgid":"94f63726e95a2b79"}`

and I am seeing this error from the `JSON` node:

`Unexpected end of JSON input`

Now, I know I have the `JSON` node set to autodetect.  
I fear I have messed up in the message structure, but can't be sure.

---

<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:** [7 December 2021 08:04 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/2 "2021-12-07T08:04:44Z")

</div>

The JSON nodes converts `msg.payload`, which should be an object or a JSON string. Your payload is just a simple string, "Online".

---

<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:** [7 December 2021 08:06 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/3 "2021-12-07T08:06:42Z")

</div>

So what do I do if the payload varies between _simple text_ and complicated stuff?

I'm guessing this problem is biting me in many places because the messages coming in vary from simple stuff to complex/structures payloads.

😕

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [7 December 2021 08:26 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/4 "2021-12-07T08:26:05Z")

</div>

Use Catch node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46257ece45b856dfaf9ad7662aff19be97542535.png)

Configure the change node after that, to determine if it has LWT in topic.

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

So you know that the payload will be string (Online or Offline) and then use that payload as you'll need.

---

<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:** [7 December 2021 08:30 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/5 "2021-12-07T08:30:20Z")

</div>

Thanks for that.

Not to dismiss what you said - indulge me - so having only `ARLEC-1/#` as the topic is asking for trouble?

Would it save me a lot of hurt if I resolved it down another _level_ in the topic?  
That way I get around some messages being `JSON` or plain text.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [7 December 2021 08:58 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/6 "2021-12-07T08:58:57Z")

</div>

Why not try `ARLEC-1/tele/LWT` as topic? I would assume you would then get strings only in the payload

---

<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:** [7 December 2021 09:06 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/7 "2021-12-07T09:06:16Z")

</div>

TBH, I am a bit overwhelmed with this.

I think most of the time I have better `topic` definition but there are a couple of nodes which are like this one here in this example.

I'm not exactly sure why, maybe only for _debuging_ but now and then they _complain_ (well, the `JSON` node after them) about the message.

Could I get around this problem in a similar way (no offence @hotNipi) with this kind of set up:

 ![Screenshot from 2021-12-07 20-04-24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/2887dac835a64e1d5a8444c5157a09acbc19f6f3.png)

and the switch node is set as

 ![Screenshot from 2021-12-07 20-04-36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/2404ef25ba1b6818202981e80198196abfac7b4e.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:** [7 December 2021 09:39 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/8 "2021-12-07T09:39:17Z")

</div>

> [@Trying\_to\_learn](#):
>
> Would it save me a lot of hurt if I resolved it down another _level_ in the topic

If different topics require different processing then use separate MQTT nodes. Then on the ones that are objects you can specify the output as the result of converting the JSON to an object.

---

<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:** [7 December 2021 09:45 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/9 "2021-12-07T09:45:54Z")

</div>

Yes. So under the example `ARLEC-1/#` I am receiving `LWT` and other messages.  
One is plain text and another is a JSON message.

So (going further down this thread) should I resolve the topic better so I separate the different types of messages?

But if I leave the example posted - maybe just to see the messages at the higher level - would that example I posted work? (Though it seems to be.) But I am known to be wrong. 😉

---

<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:** [7 December 2021 10:24 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/10 "2021-12-07T10:24:07Z")

</div>

> [@Trying\_to\_learn](#):
>
> Yes. So under the example `ARLEC-1/#` I am receiving `LWT` and other messages.  
> One is plain text and another is a JSON message.

As I said, if they need different processing (as they do in that case) then use different MQTT nodes, so you can configure one as string and the other as parsed json.

---

<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:** [7 December 2021 10:35 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/11 "2021-12-07T10:35:47Z")

</div>

Or the alternative is to use one MQTT node and a Switch node to send the different topics down different paths, whichever is best for your particular system.

---

<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:** [5 February 2022 10:36 UTC](https://discourse.nodered.org/t/i-am-missing-something-here-with-message-format-json/54814/12 "2022-02-05T10:36:35Z")

</div>

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