How to convert string to object in change node?

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.

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)

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.

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

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

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

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

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.

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

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