# How to convert string to object in change node?

**URL:** <https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650>\
**Category:** General\
**Created:** [24 January 2021 15:43 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650 "2021-01-24T15:43:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nolram12345](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nolram12345/32/35840_2.png) [@Nolram12345](https://discourse.nodered.org/u/Nolram12345)\
**Post date:** [24 January 2021 15:43 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/1 "2021-01-24T15:43:30Z")

</div>

Hello, I am quite new to Node-RED so excuse this probably quite obvious question.  
I have the requirement of converting a string msg.payload to an object I can then further process, as currently I get the error _Cannot set property of non-object type_ when trying to further process this string in another change node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 January 2021 16:31 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/2 "2021-01-24T16:31:18Z")

</div>

Hi. What is in the string?

Can you show us the string value (screenshot of debug or use the copy button that appears when you hover over the payload in the debug window)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 January 2021 16:33 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/3 "2021-01-24T16:33:22Z")

</div>

Payload has been set as string type,. So to change it to an object you would need to set the payload property directly, or move it in the change node.  
i.e.

```auto
[{"id":"b544ec9e.88087","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12345678","payloadType":"str","x":160,"y":2700,"wires":[["8902e72b.dd6b58"]]},{"id":"8902e72b.dd6b58","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"payload.test","pt":"msg","to":"12345678","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":2700,"wires":[["1eb726ff.ccd5e9"]]},{"id":"1eb726ff.ccd5e9","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":2700,"wires":[]},{"id":"b74cd163.02bac","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"test\":\"12345678\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":2780,"wires":[["1eb726ff.ccd5e9"]]},{"id":"bd95a857.082a3","type":"change","z":"57675e72.26a0d8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2840,"wires":[["1eb726ff.ccd5e9"]]},{"id":"52b2f54a.f192b4","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12345678","payloadType":"str","x":150,"y":2780,"wires":[["b74cd163.02bac"]]},{"id":"86203699.e1081","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12345678","payloadType":"str","x":150,"y":2840,"wires":[["bd95a857.082a3"]]}]

```

The first inject shows the error, I am guessing you are having. The others show how to set the property to an object.

---

<div class="post-metadata">

**Author:** ![Nolram12345](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nolram12345/32/35840_2.png) [@Nolram12345](https://discourse.nodered.org/u/Nolram12345)\
**Post date:** [25 January 2021 07:24 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/4 "2021-01-25T07:24:29Z")

</div>

I got the input from a dashboard text input, so it can change.

---

<div class="post-metadata">

**Author:** ![Nolram12345](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nolram12345/32/35840_2.png) [@Nolram12345](https://discourse.nodered.org/u/Nolram12345)\
**Post date:** [25 January 2021 07:34 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/5 "2021-01-25T07:34:01Z")

</div>

It is the same error, but from something else. Take a look at this example :

```auto
[{"id":"1e35786f.732cc8","type":"function","z":"ac84f979.d3df98","name":"Text transformation","func":"var msg = { payload: \"Thing : \" + msg.payload + \" - I think thats a thing\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":740,"wires":[["ab4f4b7a.7d90a8","fe0d584b.9be2e"]]},{"id":"fe0d584b.9be2e","type":"change","z":"ac84f979.d3df98","name":"Transform into payload.test from payload","rules":[{"t":"set","p":"payload.test","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":740,"wires":[["5ac2b4c3.9bda74","8180c3d1.bbfa"]]},{"id":"ab4f4b7a.7d90a8","type":"debug","z":"ac84f979.d3df98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":800,"wires":[]},{"id":"50a908c2.590478","type":"inject","z":"ac84f979.d3df98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":740,"wires":[["1e35786f.732cc8"]]},{"id":"5ac2b4c3.9bda74","type":"debug","z":"ac84f979.d3df98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.test","targetType":"msg","statusVal":"","statusType":"auto","x":1250,"y":740,"wires":[]},{"id":"cf01f934.ed0dc","type":"comment","z":"ac84f979.d3df98","name":"Error occurs here","info":"","x":890,"y":700,"wires":[]},{"id":"8180c3d1.bbfa","type":"debug","z":"ac84f979.d3df98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":780,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 January 2021 07:44 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/6 "2021-01-25T07:44:15Z")

</div>

As said before you set the property of payload to string type, with value of timestamp.  
So when you try to add .test to the payload string type it throughs error. As it can not add an object property to a string property. You need to move the contents of payload to payload test, which will set payload as an object.

```auto
[{"id":"6ee7e31e.295814","type":"function","z":"57675e72.26a0d8","name":"Text transformation","func":"var msg = { payload: \"Thing : \" + msg.payload + \" - I think thats a thing\"};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":2940,"wires":[["495a2fce.f5b8c8","6530c869.bcffd"]]},{"id":"495a2fce.f5b8c8","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":3000,"wires":[]},{"id":"6530c869.bcffd","type":"change","z":"57675e72.26a0d8","name":"Transform into payload.test from payload","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":2940,"wires":[["209c2a6b.651876","b3611dc2.ace18"]]},{"id":"909fa323.0cfa1","type":"inject","z":"57675e72.26a0d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":3000,"wires":[["6ee7e31e.295814"]]},{"id":"209c2a6b.651876","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.test","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":2940,"wires":[]},{"id":"b3611dc2.ace18","type":"debug","z":"57675e72.26a0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":2980,"wires":[]}]

```

---

<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:** [26 March 2021 07:45 UTC](https://discourse.nodered.org/t/how-to-convert-string-to-object-in-change-node/39650/7 "2021-03-26T07:45:12Z")

</div>

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