# Help to build a JSON packet

**URL:** <https://discourse.nodered.org/t/help-to-build-a-json-packet/20288>\
**Category:** General\
**Created:** [13 January 2020 22:39 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288 "2020-01-13T22:39:02Z")\
**Posts on this page:** 5\
**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:** [13 January 2020 22:39 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288/1 "2020-01-13T22:39:03Z")

</div>

(Yes, I'm probably a gluten for punishment.)

I'm messing around with sending a message over MQTT and need extra things in the message.

The basic system works, but when I try to add extra stuff on the message, it all falls over.

From memory, `JSON` takes a _complex message_ and builds a tree on the `payload` part.  
So: `msg.something` becomes `msg.payload.something` And so on.

This is the basic code, but the `msg.delay` isn't getting through.

```auto
[{"id":"99948203.32add","type":"mqtt out","z":"cc585130.29b7e8","name":"","topic":"PFC_LCD","qos":"","retain":"","broker":"c97b6780.72589","x":710,"y":740,"wires":[]},{"id":"7bc183d.305aefc","type":"inject","z":"cc585130.29b7e8","name":"","topic":"","payload":"1,1:\"This is a test\"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":740,"wires":[["cceb8bff.745918"]]},{"id":"cceb8bff.745918","type":"change","z":"cc585130.29b7e8","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":740,"wires":[["99948203.32add"]]},{"id":"c97b6780.72589","type":"mqtt-broker","z":"","name":"MQTT Host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"20","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"'Awaiting PiFace'","closeTopic":"","closePayload":"","willTopic":"EOM","willQos":"0","willPayload":"'PiFace telemetry failure'"}]

```

Because the `msg.payload` is: `{"_msgid":"a6ceb2a3.0ce5f","topic":"","payload":"1,1:\"This is a test\"","delay":5}`

So putting a `JSON` node in it like so:

```auto
[{"id":"99948203.32add","type":"mqtt out","z":"cc585130.29b7e8","name":"","topic":"PFC_LCD","qos":"","retain":"","broker":"c97b6780.72589","x":830,"y":740,"wires":[]},{"id":"7bc183d.305aefc","type":"inject","z":"cc585130.29b7e8","name":"","topic":"","payload":"1,1:\"This is a test\"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":740,"wires":[["cceb8bff.745918"]]},{"id":"cceb8bff.745918","type":"change","z":"cc585130.29b7e8","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":740,"wires":[["82472447.89f28","6beaafb2.b4312"]]},{"id":"6beaafb2.b4312","type":"json","z":"cc585130.29b7e8","name":"","property":"payload","action":"","pretty":false,"x":650,"y":740,"wires":[["99948203.32add"]]},{"id":"c97b6780.72589","type":"mqtt-broker","z":"","name":"MQTT Host","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"20","cleansession":true,"birthTopic":"SOM","birthQos":"2","birthPayload":"'Awaiting PiFace'","closeTopic":"","closePayload":"","willTopic":"EOM","willQos":"0","willPayload":"'PiFace telemetry failure'"}]

```

I get this error:  
`Unexpected token , in JSON at position 1`

I know: I've been told before. And I have forgotten. Sorry.

How do I get around the problem?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 January 2020 23:08 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288/2 "2020-01-13T23:08:09Z")

</div>

Your payload contains the string `1,1:"This is a test"`. As you suspect, that is not valid JSON. A JSON object looks like:

```auto
{
   "A": 1,
   "B": 2
}

```

I've formatted it over multiple lines for clarity. It is identical to the string `{"A":1, "B":2}`.

A JSON Object consists of `{ }` with a set of comma-separated key/value pairs. Each key should be a string surrounded by double quotes - `"`. The key and value are separated by a colon - `:`.

It's a bit hard to parse `1,1:"This is a test"` to suggest how it should change - which bits are keys and which are values? But I hope this gives you a pointer in the right direction.

---

<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:** [13 January 2020 23:13 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288/3 "2020-01-13T23:13:47Z")

</div>

Thank you @knolleary.

Alas (my fault) I am not wanting to make an array.

The `msg.payload` is a text string to be displayed. The 1,1 is the _start_ location for the message.

So, I am needing to send:  
`start position` (1,1) `:` `"Message"` (It has "" around it because of space parsing.)  
On top of that, I am wanting to send a `msg.delay` property also.

In the mean time I dug through my docs, and (my bad) the `JSON` node doesn't construct the format. It only pulls it apart.

So I have to stick a `function` node in before the `MQTT` node to construct the format like:  
`msg.payload = {payload:msg.payload, delay:msg.delay, position:msg.position }`  
(Great, C/R not translated.)

Sorry. My bad/fault.  
(But that's status quo for me.)

---

<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:** [13 January 2020 23:31 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288/4 "2020-01-13T23:31:32Z")

</div>

(Just so it keeps my replies separate and may help if you are reading the other reply now.)

Machine 1:

- I want to send a message via `mqtt`.
- The message is a string which may (or not) contain spaces.
- It needs a _starting position_ - though I may make it that if none is specified it defaults to 1,1.
- A _delay_ (Time to live) is also optional - though probably usually used.  
This controls how long the message is displayed.  
If there is no _delay_ it is _forever_. Otherwise it is how many seconds.

So, the first idea is to send a `msg.payload` something like:  
`1,1:"This is a test message to be displayed on the screen"`  
and a `msg.delay` of:  
`5` (Seconds) I know, it is milli-seconds in the `delay` node, but that's easily worked around.

That wasn't working because of the `1,1:` prefix.

So I thought I would make it "easier" in that it would be like this:  
`msg.payload` = `This is a test message to be displayed on the screen`  
`msg.position` = `1,1`  
`msg.delay` = `5`

Then construct a `mqtt` compatible message and send it.

That sort of works.  
But - now - at the other end, there are all these `"` around things and the `1,1` is text not numbers.

Yeah, my problem. I am just annoyed - with myself - for not remembering how it is done.

I seem to be cursed with solving things the **most complicated** way first. (Nearly always)

So it is a lot of work to get to the real - most optimal - answer.

I'm guessing I am going to have to split the `msg.position` into two parts and `parsInt( )` them.

But that's just ahead of where I am.

I need to get rid of the extra (extraneous?) `"` around things.

---

<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:** [14 January 2020 04:51 UTC](https://discourse.nodered.org/t/help-to-build-a-json-packet/20288/5 "2020-01-14T04:51:10Z")

</div>

Only by repeated _banging head against wall_ did I get it working.

Not too complicated, but gee it was painful.
