String to object

Hi Everyone,
I have been playing around with Node-Red on and off for while but only pretty basic stuff. I am now working on a project that I want to use to expand my skills in this area. This involves using it to communicate with a radio device over RS232 using basic text commands. I have been successful in creating a basic flow to send a command to it and get a response. Now I want to take that text response and turn it into an object. When I send a "stat" command I receive back the following:-
[local]
noise=0
power=0
rssi=0
temp=104
batt=130
I would like to format this as an object like this:-
{"noise":0,"power":0,"rssi":0,temp:104,"batt":130}

I am not sure if there are existing nodes that would be appropriate for this or if i will need to use a function node and write in JS, which I have no experience in.
Some help on getting started with this would be great.

[{"id":"2b4be924.866df6","type":"serial in","z":"1c4f9602.c5248a","name":"com 7 out","serial":"ffbf433b.acf54","x":551.7812347412109,"y":655.900520324707,"wires":[["e18f50a.323d9b"]]},{"id":"8010f34b.2cc26","type":"serial out","z":"1c4f9602.c5248a","name":"com7 in","serial":"ffbf433b.acf54","x":463.78690338134766,"y":752.1619396209717,"wires":[]},{"id":"ca3ef3d2.ffa8f","type":"function","z":"1c4f9602.c5248a","name":"","func":"msg.payload = \"stat\\r\";\nreturn msg;","outputs":1,"noerr":0,"x":297.0142402648926,"y":803.0056781768799,"wires":[["8010f34b.2cc26"]]},{"id":"e18f50a.323d9b","type":"debug","z":"1c4f9602.c5248a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":955.7925262451172,"y":850.4232006072998,"wires":[]},{"id":"b10d5b7.decd8a8","type":"inject","z":"1c4f9602.c5248a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":129.79261779785156,"y":674.2386245727539,"wires":[["ca3ef3d2.ffa8f"]]},{"id":"ffbf433b.acf54","type":"serial-port","z":"","serialport":"com7","serialbaud":"38400","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"100","bin":"false","out":"interbyte","addchar":"","responsetimeout":"10000"}]

Hi Darren,

I would go with JS .. maybe there's an simpler way.

let arr1 = msg.payload.split(' ')
arr1.shift()   // remove [local]
let obj= {}

arr1.forEach(el => {
    let arr2 = el.split('=')
    obj[arr2[0]] = Number(arr2[1])
    //node.warn(arr2)
})

msg.payload = obj
return msg;

Test flow :

[{"id":"60bc45d9.4b9474","type":"inject","z":"244d1116.0757be","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[local] noise=0 power=0 rssi=0 temp=104 batt=130","payloadType":"str","x":360,"y":400,"wires":[["5d01878e.87b468"]]},{"id":"5d01878e.87b468","type":"function","z":"244d1116.0757be","name":"","func":"let arr1 = msg.payload.split(' ')\narr1.shift()   // remove [local]\nlet obj= {}\n\narr1.forEach(el => {\n    let arr2 = el.split('=')\n    obj[arr2[0]] = Number(arr2[1])\n    //node.warn(arr2)\n})\n\nmsg.payload = obj\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":400,"wires":[["7a755d59.59939c"]]},{"id":"7a755d59.59939c","type":"debug","z":"244d1116.0757be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":400,"wires":[]}]

ps. im not sure how each line is split .. i used a space in the above example .. maybe its a newline character?

2 Likes

Thanks heaps, that works fantastically.
I really appreciate the help

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