Sounds like a cross between a switch
and a change
node... a type of "splitter" function that sends parts of an object to different output ports. Of course, this is doable using a function node with a bit of code, like this:
var keys = Object.keys(msg.payload);
var msgs = [];
for (var ii = 0; ii < keys.length; ii++) {
var key = keys[ii];
msgs[ii] = {
"topic": "output-" + ii,
"payload": msg.payload[key]
}
}
return msgs;
Note: You would have to know how many outputs are expected, and set that on the function
node prior to processing a msg, since the # of ports cannot be set dynamically.
(sample flow 1)
[{"id":"5f9f01e4.cae49","type":"inject","z":"58c8eb7a.5496c4","name":"{ a:1, b:2, c:3, d:4 }","topic":"","payload":"{\"a\":1,\"b\":2,\"c\":3,\"d\":4}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":3840,"wires":[["d429c1e0.31fd2"]]},{"id":"d429c1e0.31fd2","type":"function","z":"58c8eb7a.5496c4","name":"splitter","func":"var keys = Object.keys(msg.payload);\nvar msgs = [];\n\nfor (var ii = 0; ii < keys.length; ii++) {\n var key = keys[ii];\n msgs[ii] = {\n \"topic\": \"output-\" + ii,\n \"payload\": msg.payload[key]\n }\n}\n\nreturn msgs;","outputs":3,"noerr":0,"x":390,"y":3840,"wires":[["49805183.4177a"],["5431b04a.5dc02"],["5a6d49c9.335638"]],"outputLabels":["\"a\"","\"b\"","\"c\""]},{"id":"49805183.4177a","type":"debug","z":"58c8eb7a.5496c4","name":"port \"a\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":580,"y":3780,"wires":[]},{"id":"5431b04a.5dc02","type":"debug","z":"58c8eb7a.5496c4","name":"port \"b\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":580,"y":3840,"wires":[]},{"id":"5a6d49c9.335638","type":"debug","z":"58c8eb7a.5496c4","name":"port \"c\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":580,"y":3900,"wires":[]}]
If you prefer not to write any code, you can do the same thing with a split
node, set to send each property of the payload object as its own message (using the key as the topic)...
... followed by a switch
node, that maps each msg to its output port by topic, like so:
(sample flow 2)
[{"id":"9e4fedc4.a0955","type":"switch","z":"58c8eb7a.5496c4","name":"by topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"a","vt":"str"},{"t":"eq","v":"b","vt":"str"},{"t":"eq","v":"c","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":580,"y":4040,"wires":[["d5047a75.c85468"],["d5806858.1b8b18"],["d59aa3aa.67c95"]]},{"id":"d5047a75.c85468","type":"debug","z":"58c8eb7a.5496c4","name":"topic \"a\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":760,"y":3980,"wires":[]},{"id":"d5806858.1b8b18","type":"debug","z":"58c8eb7a.5496c4","name":"topic \"b\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":760,"y":4040,"wires":[]},{"id":"d59aa3aa.67c95","type":"debug","z":"58c8eb7a.5496c4","name":"topic \"c\"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":760,"y":4100,"wires":[]},{"id":"7e995afd.888914","type":"inject","z":"58c8eb7a.5496c4","name":"{ a:1, b:2, c:3, d:4 }","topic":"","payload":"{\"a\":1,\"b\":2,\"c\":3,\"d\":4}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":4040,"wires":[["941f49cc.beb298"]]},{"id":"941f49cc.beb298","type":"split","z":"58c8eb7a.5496c4","name":"splitter","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":390,"y":4040,"wires":[["9e4fedc4.a0955"]]}]