# MQTT, payload, JSON and wanting to check

**URL:** <https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859>\
**Category:** General\
**Created:** [29 December 2022 03:22 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859 "2022-12-29T03:22:13Z")\
**Posts on this page:** 8\
**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:** [29 December 2022 03:22 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/1 "2022-12-29T03:22:13Z")

</div>

(Sort of following on from my other post/question)

What I know about `MQTT`:

1 - ~~basically~~ the `payload` is the only part of the message that is sent to the other end.  
(that's about it)

If you want to send other stuff, you need to construct a _special_ message where the `payload` is not just text.

## JSON node/s.

Way back when I first started (and this may be wrong) if you wanted to send _fancy_ mesasges through `MQTT` you would have to use a `JSON` node going in and coming out of the `MQTT` node. **WAS** (Past tense)

Seems now the newer version/s of the nodes have _auto detect_ and so you don't need to use the `JSON` nodes as before.

But I've just annoyed Sean (well: he helped me) with getting something that should be easy to work.

Given I have (example) a message like this:  
`{"payload":"2022-12-29 12:05:09 Main - Up"}`  
and  
`{"state":"Online"}`  
and I want BOTH of these to go through `MQTT`. _How do I do it?_

Seems my _old_ way works:  
Put it into a `function` node - code:

```auto
let msg2 = {};
msg2.payload = {"payload":msg.payload,"state":msg.state};
return msg2;

```

Then at the other end I get:

`{"topic":"test","payload":{"payload":"2022-12-29 12:05:09 Main - Up","state":"Up"},"qos":0,"retain":false,"_topic":"test","_msgid":"d7f246aadb65e10b"}`

But that isn't usable to me.  
I want a `payload` and `state`. Not `payload.payload` and `payload.state`.  
I **seem to remember** that the `JSON` node was the answer. But it is no longer.  
I have to basically do the opposite to what I did at the other end.

```auto
let msg2 = {};
msg2 = msg.payload;
return msg2;

```

And I guess: _Fair enough_.

But I can't believe that .... 4 years ago (nominal, maybe more) I got this right and BEFORE it was how to do it.

No: I'm not boasting. I'm confused.

Where's the _elephant_ I'm missing?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [29 December 2022 03:41 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/2 "2022-12-29T03:41:19Z")

</div>

> [@Trying\_to\_learn](#):
>
> If you want to send other stuff, you need to construct a _special_ message where the `payload` is not just text.

This is correct - the payload can be a single value OR an object (containing more values) but only one or the other, not both.

---

<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:** [29 December 2022 03:42 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/3 "2022-12-29T03:42:28Z")

</div>

So something like what I showed (here) is the _solution_?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [29 December 2022 03:52 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/4 "2022-12-29T03:52:13Z")

</div>

Personally I would try to avoid having msg.payload.payload.

"payload" is the [only] element of a message which is transmitted through MQTT, but within payload there can be any number of elements.  
I think their names should describe what the data is, and another "payload" has no place here.

Further, within

```auto
{
"payload":"2022-12-29 12:05:09 Main - Up"
}

```

the timestamp is one data item and "Main - Up" is another, So I would change it to something like this

```auto
msg.payload = {
   "timestamp": "2022-12-29 12:05:09", 
   "event": "Main - Up", 
   "state": msg.state
}

```

When msg.payload arrives at the destination, the data is available as msg.payload.timestamp, msg.payload.event and msg.payload.state

---

<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:** [29 December 2022 04:00 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/5 "2022-12-29T04:00:52Z")

</div>

Yes, and I fully agree.

But this stuff started about...... 7 years ago.

~~Back then, I w~~

Ok, what's going on in THIS case.

Every now and then a pulse is sent and a scan of WAPs happens.  
I scan for mine and make reports.  
So the time is .... _obvious_ for what it is, and doing.  
The _event_ part is the _WAP of interest_ name.  
That is stuck on the end (text mode) of the timestamp.  
Then the Up/Down is the state.  
Again, I simply stuck it on the end of the message because back then, I only had a very basic network.

As time went on, I introduced _redundancy_ and so things got complicated.  
And I scan for multiple WAPs.  
Because now I have 3 machines running a _triumbrant_ (?) they need to share their information.  
That means sharing information.

I may have to sit down and rewrite the structure but just now that is not viable as I am still rebuilding my network. As in: layout, and addresses. I've been caught out a few times already with _old_ IP addresses not responding and wasting a bit of time trying to work out what is going on.  
So I think I need to get that part working before I do this stuff.

But thanks for the idea on the structure.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 December 2022 14:53 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/6 "2022-12-29T14:53:59Z")

</div>

> [@Trying\_to\_learn](#):
>
> {"state":"Online"}

What is the point of that in MQTT? `state` should probably be the end of a topic something like `devices/mydevice01/state` and it can then simple have a value of `online` or `offline`.

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

So above is an example from one of my ESP custom sensor platforms where topic `ESP/D1M05` has the status showing and then lower level topics have details. That way, if you are viewing the hierarchy in something like MQTT Explorer, you don't have to expand it just to see if something is online or not.

Or another example showing switch status:

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

The important info is on the main topic with sub-topics containing more details.

Only if I want to dump a load of reference info would I output JSON to MQTT as in this example:

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

In fact, a node-red flow restructures all of that data into more useful topic structures, I only keep this raw data for debugging and out of interest to see how many 433MHz devices my RFXtrx433e dongle can pick up over time! 🙂

* * *

As for the conversion, you can just output JSON direct to the MQTT out node. Then you can ask the MQTT in node to try to make sense of inputs. No JSON nodes needed any more.

---

<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:** [29 December 2022 21:01 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/7 "2022-12-29T21:01:27Z")

</div>

That looks nice too for structure.

But I may take on what was suggested with a structured `payload` containing the time, device and state as objects in it.

The MQTT only came about a fair way down the track.  
Reformatting the payload how @jbudd suggested looks nicer and better for my needs.

I am torn between keeping it simple - which is what I may have done here in THIS case - and **over complicating things** as I also do in other scenarios.

Thanks though.

---

<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:** [27 February 2023 21:02 UTC](https://discourse.nodered.org/t/mqtt-payload-json-and-wanting-to-check/72859/8 "2023-02-27T21:02:22Z")

</div>

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