How to covert row string (tcp in) into JSON object

Node (TCP in) with payload is string "GW_ID:155507366,TYPE:T&H,ID:ABC1212393,STAT:00000001,T:30.5,H:51.4%,Time:0-0-0 0:0:0". How to covert it into JSON object? It contains ":". Thanks

There are a few ways. Here is a function node version...

image

[{"id":"0cd00ebc447751fe","type":"inject","z":"08fa68797af5e8aa","name":"your tcp data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"GW_ID:155507366,TYPE:T&H,ID:ABC1212393,STAT:00000001,T:30.5,H:51.4%,Time:0-0-0 0:0:0","payloadType":"str","x":1010,"y":600,"wires":[["8b9455cb32ea989e"]]},{"id":"8b9455cb32ea989e","type":"function","z":"08fa68797af5e8aa","name":"","func":"//Example\n//GW_ID:155507366,TYPE:T&H,ID:ABC1212393,STAT:00000001,T:30.5,H:51.4%,Time:0-0-0 0:0:0\n\nconst pairs = msg.payload.split(\",\");\nconst obj = pairs.map(e => {\n    const parts = e.split(\":\");\n    return [parts[0],parts[1]]\n})\nmsg.payload = Object.fromEntries(obj);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":660,"wires":[["d59009bce634a565"]]},{"id":"d59009bce634a565","type":"debug","z":"08fa68797af5e8aa","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1060,"y":720,"wires":[]}]
1 Like

Or a low code option.

[{"id":"cf0e54d1.4a04c","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"GW_ID:155507366,TYPE:T&H,ID:ABC1212393,STAT:00000001,T:30.5,H:51.4%,Time:0-0-0 0:0:0","payloadType":"str","x":110,"y":60,"wires":[["f1a18f1.8d9c7f"]]},{"id":"f1a18f1.8d9c7f","type":"csv","z":"bf9e1e33.030598","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":false,"include_empty_strings":"","include_null_values":"","x":260,"y":60,"wires":[["b0032186.0d8fd"]]},{"id":"b0032186.0d8fd","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":390,"y":60,"wires":[["38d9a982.4d9786"]]},{"id":"38d9a982.4d9786","type":"csv","z":"bf9e1e33.030598","name":"","sep":":","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":false,"include_empty_strings":"","include_null_values":"","x":510,"y":60,"wires":[["10038373.70f735"]]},{"id":"10038373.70f735","type":"join","z":"bf9e1e33.030598","name":"","mode":"custom","build":"object","property":"payload.col2","propertyType":"msg","key":"payload.col1","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"7","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":60,"wires":[["efadf9c3.189028"]]},{"id":"efadf9c3.189028","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"move","p":"payload.col2","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":120,"wires":[["1d584829.4f3588"]]},{"id":"1d584829.4f3588","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":370,"y":240,"wires":[]}]
1 Like

Thanks to you guys, both solve my question.

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