Splitting Payload Values

So, I know there must be an easier way to do this than using a function node, which is what I am doing currently. So, I have an incoming messages which as multiple values stored as JSON data within msg.payload. But the subsequent nodes need the individual values in msg.payload. So, what I currently doing in a function node is the following:

msg1 = {"payload":msg.payload.VALUE1`};
msg2 = {"payload":msg.payload.VALUE2};
msg3 = {"payload":msg.payload.VALUE3};
msg4 = {"payload":msg.payload.VALUE4};
return [msg1,msg2,msg3,msg4]

But that doesn't copy over all the over values included inside of the msg object. I know I could start by creating the new msgs as a copy of msg but I wanted to see if there was a simpler way to tear these apart than what i am currently doing. Thanks!!

So, what I currently doing in a function node is the following:

The function node will now have 4 output ports and this is always consistent ?

Can you show an example of your flow and/or what msg goes into the function node and what you are expecting it to output ? What you are showing here could be done with a switch node and a change node (or 4)

Yes, it will have 4 outputs because I am trying to feed the the different values input in a JSON string into different messages. I know I could do it with 4 different change nodes too....but that just seems more complicated than one function node.

You couldn't really do it with a switch node because I want 4 outputs each time, not 1 output on one of 4 paths each time. It would have to be done with 4 change nodes.

An example of the msg.payload input would be:

{"VALUE1": 1, "VALUE2":2,"VALUE3":3, "VALUE4":4}

And the input for the following nodes need msg.payload to equal 1,2,3 or 4. so, splitting each json key into separate payloads.

Without a function node...

[{"id":"82ad0f7b4eefceec","type":"inject","z":"85e1791bc0cd6285","name":"4 properties","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"VALUE1\": 1, \"VALUE2\":2,\"VALUE3\":3, \"VALUE4\":4}","payloadType":"json","x":1306,"y":704,"wires":[["a395f9dbf3dc84a7"]]},{"id":"a395f9dbf3dc84a7","type":"split","z":"85e1791bc0cd6285","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":1458,"y":704,"wires":[["7fd363da1faaeae7"]]},{"id":"7fd363da1faaeae7","type":"switch","z":"85e1791bc0cd6285","name":"route by topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"VALUE1","vt":"str"},{"t":"eq","v":"VALUE2","vt":"str"},{"t":"eq","v":"VALUE3","vt":"str"},{"t":"eq","v":"VALUE4","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":1456,"y":800,"wires":[["e0d28738aa145d72"],["30985bacbb74e0fb"],["15a51cc054d8d9a2"],["38bda349fcbbba6a"]]},{"id":"e0d28738aa145d72","type":"debug","z":"85e1791bc0cd6285","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1648,"y":768,"wires":[]},{"id":"30985bacbb74e0fb","type":"debug","z":"85e1791bc0cd6285","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1648,"y":800,"wires":[]},{"id":"15a51cc054d8d9a2","type":"debug","z":"85e1791bc0cd6285","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1648,"y":832,"wires":[]},{"id":"38bda349fcbbba6a","type":"debug","z":"85e1791bc0cd6285","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1648,"y":864,"wires":[]}]



NOTE (just in case you overlooked this)

many people split a msg into to multiple outputs when really all they need to do is use the relevant property (e.g msg.payload.VALUE1) in then next node. for example...

image

As I state a couple of time, the nodes that I am using don't allow me to configure the input. For example the dashboard switch node. You have no way of configuring what that node will use for an input. It only allows msg.payload. if you have a way around that, I am all ears.

That was aimed more at future readers of the thread (I did say "many people")

No, in these cases, your only option is to move the value to msg.payload (or make a feature request to the maintainer)

Back to your original question

Did my demo flow not work for you?

That seems more complicated than 1 function node. I could have sworn I remembered reading about a node that would simply parse based on json key value. I was hoping someone might have heard of it too. Maybe it was just wishful thinking.

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