Content of a json payload gives an error

i have a injector node with json payload content:
{"command": "setuservariable", "idx": 13, "value": "x"}

and a mqtt node that all works ok.

But instead of "value": "x" i want to push an json-set instead of x, like this:
{"command": "setuservariable", "idx": 13, "value": "[{"obj":"lantaarn1","act":"On"},{"obj":"lantaarn1","act":"On"}]"}

i get an error:
"SyntaxError: Unexpected token o in JSON at position 54"

It must be possible to put anything in data-element "value" ?
can you help me i try this for days but cant get it to work!

What are you trying to have an object that contains a text string of an array

or an object that contains an array?

If it’s the second you don’t need the quotes around the square brackets

If it’s the first the quotes inside your string are interpreted as the end of the string. Try using single quotes instead.

You can use a JSON Validator site such as
https://jsonlint.com/ to check on the validity of your JSON

@thanks for your help @ukmoose. I cant get it to work, and my wishes looks so simple!
i tried myself, but it isnt working!

My injector node has the following content that is to be read in a function to create a mqtt message:
[{"obj":"lantaarn","act":"On"},{"obj":"lantaarn","act":"On"}]

this injector content wil be the content of the following message that will be sent to mqttt and domoticx

{"command": "setuservariable", "idx": 13, "value": "the injector content"}
injector content = [{"obj":"lantaarn","act":"On"},{"obj":"lantaarn","act":"On"}]

my domoticz has a global variable (id 13) that gets the exact content of the injector and then fires a script to execute. The last part is working.
I can push content to this variable but complex contant like my injector does not work...
when the injector works i put this content in IFTTT

type or paste code here
[{"id":"4310dae3.f33594","type":"function","z":"612f3ecd.84333","name":"","func":"// content of payload object to send to mqtt needs to be:\n// command: \"setuservariable\"\n// idx: 13\n// value: \"[{\"obj\":\"lantaarn\",\"act\":\"On\"},{\"obj\":\"lantaarn\",\"act\":\"On\"}]\"\n// the content off value comes from antemporary injector\n// when it all works this injector becomes a http in node\n\n// i tried the following\nmsg.payload.command = \"setuservariable\";\nmsg.payload.idx = 13;\nmsg.payload.value = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":140,"wires":[["3a92b3cd.9606ec","ca7a476f.2487f8"]]}]

Your function

msg.payload.command = "setuservariable";
msg.payload.idx = 13;
msg.payload.value = msg.payload;
return msg;

You need to declare that msg.payload is an object. In your code it would need to be an object and a value which it can't be.

msg.payload = { "value" : msg.payload};
msg.payload.command = "setuservariable";
msg.payload.idx = 13;

return msg;

you are a hero!
everything works! i can make content now in ifttt that more than one device has to be put on/off in one message!
i am SO SO HAPPY !

only thing i dont understand in your script:
msg.payload = { "value" : msg.payload};
msg.payload.command = "setuservariable";
msg.payload.idx = 13;

return msg;

is why the first rule is
msg.payload = { "value" : msg.payload};

and not
msg.payload.value = msg.payload;
alle the other declarations are also like that!

Because one way works and one way doesn't :slight_smile:

:rofl:

and that is a true point !

again, thanks for your support, I am very happy!
i am going to adjust my ifttt devices with this injector commands ...

mmm, still need a little extra help..

from within nodered it works perfect.
but from ifttt it does not work.

In my old config i used within the ifttt webhook:
a post with a json body...
and my old nodered received a json format..

is this new flow my injector is a string
i looked at iftt and i can also choose for:

  • text/plain
  • application/x www form

i tried both but via iffftt it does not work... the ifttt message does arrive!

solved it myself!
solution was:

stil use ifttt webhook post type json
convert the ifttt data with a json node after the http in node

[{"id":"612f3ecd.84333","type":"tab","label":"uservariable with json format","disabled":false,"info":""},{"id":"ca7a476f.2487f8","type":"mqtt out","z":"612f3ecd.84333","name":"","topic":"domoticz/in","qos":"","retain":"","broker":"21fd69bf.668b46","x":982,"y":281,"wires":[]},{"id":"3a92b3cd.9606ec","type":"debug","z":"612f3ecd.84333","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":962,"y":201,"wires":[]},{"id":"2d6074a9.a954ac","type":"inject","z":"612f3ecd.84333","name":"nr1_payload_on","topic":"","payload":"[{\"obj\":\"lantaarn\",\"act\":\"On\"},{\"obj\":\"Lamp tv links\",\"act\":\"On\"}]","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":340,"wires":[["4310dae3.f33594"]],"info":"[{\"obj\":\"lantaarn\",\"act\":\"On\"}]\n\n\n"},{"id":"4310dae3.f33594","type":"function","z":"612f3ecd.84333","name":"create the message","func":"msg.payload = { \"value\" : msg.payload};\nmsg.payload.command = \"setuservariable\";\nmsg.payload.idx = 13;\n\nreturn msg;","outputs":1,"noerr":0,"x":622,"y":201,"wires":[["3a92b3cd.9606ec","ca7a476f.2487f8"]]},{"id":"fdc99a96.3d24b8","type":"http in","z":"612f3ecd.84333","name":"LIVE met post json structure uservar","url":"/pvkvar2","method":"post","upload":false,"swaggerDoc":"","x":222,"y":201,"wires":[["4f2cc13e.b38a4","dff4288b.f60918"]]},{"id":"4f2cc13e.b38a4","type":"http response","z":"612f3ecd.84333","name":"responce post","statusCode":"","headers":{},"x":630,"y":80,"wires":[]},{"id":"ab894ebd.31e32","type":"inject","z":"612f3ecd.84333","name":"nr2_payload_off","topic":"","payload":"[{\"obj\":\"lantaarn\",\"act\":\"Off\"},{\"obj\":\"Lamp tv links\",\"act\":\"Off\"}]","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":400,"wires":[["4310dae3.f33594"]]},{"id":"8991a61d.3e3a18","type":"inject","z":"612f3ecd.84333","name":"schakelgroep sfeerverlichting aan","topic":"","payload":"[{\"obj\":\"Schakelgroepen\",\"lbl\":120}]","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":460,"wires":[["4310dae3.f33594"]]},{"id":"dff4288b.f60918","type":"json","z":"612f3ecd.84333","name":"","property":"payload","action":"","pretty":false,"x":390,"y":100,"wires":[["4310dae3.f33594"]]},{"id":"21fd69bf.668b46","type":"mqtt-broker","z":"","name":"mqttdomoticz","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

well done... you're off the bottom rung of the ladder now.

2 Likes