# Content of a json payload gives an error

**URL:** https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583
**Category:** General
**Created:** [21 August 2019 05:59 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583 "2019-08-21T05:59:55Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 05:59 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/1 "2019-08-21T05:59:55Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 August 2019 06:38 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/2 "2019-08-21T06:38:27Z")

</div>

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/](https://jsonlint.com/) to check on the validity of your JSON

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 07:12 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/3 "2019-08-21T07:12:27Z")

</div>

@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

```auto
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"]]}]

```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 August 2019 07:23 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/4 "2019-08-21T07:23:19Z")

</div>

> [@pvanklink](#):
>
> {"command": "setuservariable", "idx": 13, **"value": "the injector content"}**  
> injector content = [{"obj":"lantaarn","act":"On"},{"obj":"lantaarn","act":"On"}]

Your function

```auto
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.

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

return msg;

```

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 07:36 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/5 "2019-08-21T07:36:06Z")

</div>

> [@ukmoose](#):
>
> 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!

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [21 August 2019 07:42 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/6 "2019-08-21T07:42:37Z")

</div>

Because one way works and one way doesn't 🙂

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 08:10 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/7 "2019-08-21T08:10:21Z")

</div>

🤣

and that is a true point !

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 08:11 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/8 "2019-08-21T08:11:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 08:46 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/9 "2019-08-21T08:46:57Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [21 August 2019 08:52 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/10 "2019-08-21T08:52:41Z")

</div>

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

```auto
[{"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":""}]

```

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [21 August 2019 18:22 UTC](https://discourse.nodered.org/t/content-of-a-json-payload-gives-an-error/14583/11 "2019-08-21T18:22:49Z")

</div>

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