Help to build a JSON packet

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

[{"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:

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

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

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

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

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

Only by repeated banging head against wall did I get it working.

Not too complicated, but gee it was painful.