I'm trying to use split to separate the content, but it doesn't meet my needs

hello
I'm browsing the course
https://nodered.org/docs/user-guide/messages#understanding-the-structure-of-a-message
I have a TCP connection originating content that looks like JSON text, but I have tried many ways to convert it to an object but failed. I tried to output it separately by each "" category but couldn't
Can you help me

I forgot to upload my work content

[{"id":"ac980724.3f2b2","type":"tab","label":"流程 1","disabled":false,"info":""},{"id":"1dad182f.f500f","type":"tcp in","z":"ac980724.3f2b2","name":"","server":"server","host":"","port":"6001","datamode":"stream","datatype":"buffer","newline":"","topic":"","base64":false,"x":120,"y":220,"wires":[["fec1f053.4706f"]]},{"id":"1c8943f7.9767dc","type":"switch","z":"ac980724.3f2b2","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"relay1","vt":"str"},{"t":"eq","v":"relay2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":340,"wires":[["5deab8c3.ec80e"],["6d14aec.ab3d35"]]},{"id":"27460140.bc5386","type":"debug","z":"ac980724.3f2b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":400,"wires":[]},{"id":"fec1f053.4706f","type":"debug","z":"ac980724.3f2b2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":220,"wires":[]},{"id":"35bfe847.cfcd98","type":"debug","z":"ac980724.3f2b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":340,"wires":[]},{"id":"1f9b4327.f9cccd","type":"split","z":"ac980724.3f2b2","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":400,"wires":[["27460140.bc5386","1c8943f7.9767dc"]]},{"id":"30a7c936.cad01e","type":"inject","z":"ac980724.3f2b2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[123,34,102,97,110,45,86,34,58,49,50,46,51,57,57,57,44,34,108,111,97,100,45,86,34,58,49,50,46,49,44,34,114,101,108,97,121,50,34,58,49,44,34,102,97,110,45,65,34,58,48,44,34,115,111,108,111,97,114,45,65,34,58,48,44,34,114,101,108,97,121,52,34,58,48,44,34,114,101,108,97,121,49,34,58,49,44,34,114,101,108,97,121,51,34,58,48,44,34,115,111,108,111,97,114,45,86,34,58,48,44,34,114,101,108,97,121,53,34,58,48,125]","payloadType":"bin","x":290,"y":400,"wires":[["1f9b4327.f9cccd","35bfe847.cfcd98"]]},{"id":"5deab8c3.ec80e","type":"debug","z":"ac980724.3f2b2","name":"relay1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":280,"wires":[]},{"id":"6d14aec.ab3d35","type":"debug","z":"ac980724.3f2b2","name":"relay2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":340,"wires":[]}]

Have you tried the JSON node? That will parse the JSON string and convert it a JavaScript object you can use to access the properties of.

I tried the JSON node but it output the same output or Ignore payloads that are not in object format

Try

[{"id":"30a7c936.cad01e","type":"inject","z":"ac980724.3f2b2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[123,34,102,97,110,45,86,34,58,49,50,46,51,57,57,57,44,34,108,111,97,100,45,86,34,58,49,50,46,49,44,34,114,101,108,97,121,50,34,58,49,44,34,102,97,110,45,65,34,58,48,44,34,115,111,108,111,97,114,45,65,34,58,48,44,34,114,101,108,97,121,52,34,58,48,44,34,114,101,108,97,121,49,34,58,49,44,34,114,101,108,97,121,51,34,58,48,44,34,115,111,108,111,97,114,45,86,34,58,48,44,34,114,101,108,97,121,53,34,58,48,125]","payloadType":"bin","x":290,"y":400,"wires":[["f25dbb66.7bd728"]]},{"id":"f25dbb66.7bd728","type":"function","z":"ac980724.3f2b2","name":"","func":"msg.payload =  JSON.parse(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":340,"wires":[["35bfe847.cfcd98"]]},{"id":"35bfe847.cfcd98","type":"debug","z":"ac980724.3f2b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":370,"y":300,"wires":[]}]
msg.payload =  JSON.parse(msg.payload);
return msg;

Oh, my God, this is perfect
Why didn't I think of using this method, thank you very much

The issue with the JSON node is you have a Buffer object, not a JSON string - and the JSON node doesn't know what to do with it.

We should look at handling that better.

1 Like