Extracted from a part of a chain

Hello.
I am starting with nodeRed.
I am looking for how it can be extracted from a part of a chain,
I expose the message that is received. "C, *, 1234, Z". I want to get the value '1234'.
How should I do it in a function?
Where can I find documentation for working with strings in NODERED?
Thanks and regards.

One method would be to use the csv parser node
image

and then pick out the 3rd item in the resulting array

1 Like

Why are all of you want to extract somehting with a function node - writing code. Node Red has its strength by using nodes:

[{"id":"7f8eb7fe4b3bf817","type":"inject","z":"cb05e14b.326a9","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"C, *, 1234, Z","payloadType":"str","x":690,"y":580,"wires":[["e7bbf036233cfeed"]]},{"id":"e7bbf036233cfeed","type":"split","z":"cb05e14b.326a9","name":"","splt":",","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":870,"y":580,"wires":[["1bc94331a3c6cc17"]]},{"id":"93be156fa87a1d48","type":"debug","z":"cb05e14b.326a9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1190,"y":580,"wires":[]},{"id":"1bc94331a3c6cc17","type":"switch","z":"cb05e14b.326a9","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1020,"y":580,"wires":[["93be156fa87a1d48"]]}]

Because it simpler and allows to trim or convert to a number etc if needed and It is often quicker than sending 4 messages when one will do.
@Manu110 here ar examples of using Javascript and JSONata for the OP

[{"id":"d3712560.b7de8","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"C, *, 1234, Z","payloadType":"str","x":160,"y":1160,"wires":[["65da86a0.e11e08"]]},{"id":"65da86a0.e11e08","type":"function","z":"c791cbc0.84f648","name":"","func":"msg.payload = msg.payload.split(\",\")[2].trim()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":1160,"wires":[["98730897.18ec28"]]},{"id":"98730897.18ec28","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":1160,"wires":[]},{"id":"7f8eb7fe4b3bf817","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"C, *, 1234, Z","payloadType":"str","x":160,"y":1060,"wires":[["cd5bff6.e8a8d"]]},{"id":"cd5bff6.e8a8d","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$trim($split($$.payload,\",\")[2])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":1060,"wires":[["93be156fa87a1d48"]]},{"id":"93be156fa87a1d48","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":1060,"wires":[]}]

Ok then we can code all our flows in one function node and then only in-Nodes and out-Nodes messages will be sent. :wink:

1 Like

If you don't want to use function and code that's fine. P.s. Your flow results in 4 times the number of messages sent and still you have not parsed the "1234" correctly.

@cymplecy I like your thought. You could also target the specific string as below

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