Loop through Json - multiple messages, multiple outputs

Hi! I wonder if there is a loop style way to distribute values from json to multiple function node outputs, instead of fixed solution.

I have a json that can carry maximum 10 objects (0-9) however it can be shorter (like 6 objects for instance) and it also can have a key T or might not have. So I need to check for key T in each object and if found pass it to corresponding (0-9) node output. All in all whatever json content arrives my code distributes needed values to particular outputs. Outputs will be connected to charts.

So in my mind JS code might look somewhat:

for (var i = 0; i < msg.payload.length; i++)
if (msg.payload[i] && msg.payload[i].T) { //check for key presence in json
    msg  = { payload: msg.payload[i].T, topic: [i] }; //make first message if the key exists in json 
//something here - to pass this message to output [i] +1 or assign the msg to this output 
}

I have read Writing Functions : Node-RED on multiple outputs/multiple messages but this does not seems a way to go. Also tried to look for similar examples but did not found solution for that.

My json looks like:

{"0":{"T":26.5,"R":0,"P":26,"L":1},"1":{"T":26.6,"R":0,"P":26,"L":1},"2":{"T":26.7,"R":0,"P":26,"L":2},"3":{"R":0},"4":{"R":0}}

Here's one way to do it:
Untitled 2

[{"id":"97f9ce8525c53d2d","type":"inject","z":"6b7503f72b16f50f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"0\":{\"T\":26.5,\"R\":0,\"P\":26,\"L\":1},\"1\":{\"T\":26.6,\"R\":0,\"P\":26,\"L\":1},\"2\":{\"T\":26.7,\"R\":0,\"P\":26,\"L\":2},\"3\":{\"R\":0},\"4\":{\"R\":0}}","payloadType":"json","x":170,"y":280,"wires":[["58eb6df9e9dda46c"]]},{"id":"58eb6df9e9dda46c","type":"function","z":"6b7503f72b16f50f","name":"function 4","func":"var newmsg = {}\nfor (var foo in msg.payload) {\n    if (msg.payload[foo].hasOwnProperty(\"T\")) {\n        newmsg.payload = msg.payload[foo]\n        newmsg.payload.index = foo\n        node.send (newmsg)\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":280,"wires":[["964d98f0afa27058"]]},{"id":"4a17fd0d381afdbf","type":"debug","z":"6b7503f72b16f50f","name":"index 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":100,"wires":[]},{"id":"964d98f0afa27058","type":"switch","z":"6b7503f72b16f50f","name":"","property":"payload.index","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"},{"t":"eq","v":"7","vt":"num"},{"t":"eq","v":"8","vt":"num"},{"t":"eq","v":"9","vt":"num"}],"checkall":"true","repair":false,"outputs":10,"x":470,"y":280,"wires":[["4a17fd0d381afdbf"],["a403909234210974"],["433e07a361008062"],["2544dd1a900a9041"],["1c58cf42b6078063"],["470283b26fd0ecbf"],["46b470f5d4a583a0"],["4ac51608fd6a5806"],["2c2c40c126b7aa6c"],["ed705d284e9ae398"]]},{"id":"a403909234210974","type":"debug","z":"6b7503f72b16f50f","name":"index 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":140,"wires":[]},{"id":"433e07a361008062","type":"debug","z":"6b7503f72b16f50f","name":"index 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":180,"wires":[]},{"id":"2544dd1a900a9041","type":"debug","z":"6b7503f72b16f50f","name":"index 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":220,"wires":[]},{"id":"1c58cf42b6078063","type":"debug","z":"6b7503f72b16f50f","name":"index 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":260,"wires":[]},{"id":"470283b26fd0ecbf","type":"debug","z":"6b7503f72b16f50f","name":"indec 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":300,"wires":[]},{"id":"46b470f5d4a583a0","type":"debug","z":"6b7503f72b16f50f","name":"index 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":340,"wires":[]},{"id":"4ac51608fd6a5806","type":"debug","z":"6b7503f72b16f50f","name":"index 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":380,"wires":[]},{"id":"2c2c40c126b7aa6c","type":"debug","z":"6b7503f72b16f50f","name":"index 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":420,"wires":[]},{"id":"ed705d284e9ae398","type":"debug","z":"6b7503f72b16f50f","name":"index 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":460,"wires":[]}]

If you want to have each value of your object in a separate message, I have a complete subflow, if this is what you want. The origin of the value is in the topic, so you can translate the topics tonwhat you need in the chart nodes.

A function node with 10 outputs and this function should work

const newmsg = [null,null,null,null,null,null,null,null,null,null]
Object.entries(msg.payload).forEach(arr => {
    if (arr[1].hasOwnProperty("T")) {
        newmsg[arr[0]] = {payload: arr[1]};
    }
})
return newmsg;

Example flow

[{"id":"97f9ce8525c53d2d","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"0\":{\"T\":26.5,\"R\":0,\"P\":26,\"L\":1},\"3\":{\"T\":26.6,\"R\":0,\"P\":26,\"L\":1},\"4\":{\"T\":26.7,\"R\":0,\"P\":26,\"L\":2},\"5\":{\"R\":0},\"6\":{\"R\":0}}","payloadType":"json","x":370,"y":4720,"wires":[["58eb6df9e9dda46c"]]},{"id":"58eb6df9e9dda46c","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 4","func":"const newmsg = [null,null,null,null,null,null,null,null,null,null]\nObject.entries(msg.payload).forEach(arr => {\n    if (arr[1].hasOwnProperty(\"T\")) {\n        newmsg[arr[0]] = {payload: arr[1]};\n    }\n})\nreturn newmsg;","outputs":10,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":4720,"wires":[["4a17fd0d381afdbf"],["a403909234210974"],["433e07a361008062"],["2544dd1a900a9041"],["1c58cf42b6078063"],["470283b26fd0ecbf"],["46b470f5d4a583a0"],["4ac51608fd6a5806"],["2c2c40c126b7aa6c"],["ed705d284e9ae398"]]},{"id":"4a17fd0d381afdbf","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4540,"wires":[]},{"id":"a403909234210974","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4580,"wires":[]},{"id":"433e07a361008062","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4620,"wires":[]},{"id":"2544dd1a900a9041","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4660,"wires":[]},{"id":"1c58cf42b6078063","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4700,"wires":[]},{"id":"470283b26fd0ecbf","type":"debug","z":"da8a6ef0b3c9a5c8","name":"indec 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4740,"wires":[]},{"id":"46b470f5d4a583a0","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4780,"wires":[]},{"id":"4ac51608fd6a5806","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4820,"wires":[]},{"id":"2c2c40c126b7aa6c","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4860,"wires":[]},{"id":"ed705d284e9ae398","type":"debug","z":"da8a6ef0b3c9a5c8","name":"index 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":4900,"wires":[]}]

@E1cid Wow! So compact and great! I spent 5 minutes to figure out why your code gives the output to 0, 3 & 4 instead of 0,1,2 and then I realised that You changed Json to challenge the code. :slight_smile:
Can not imagine shorter and more elegant way of solution! Seems to be bullet proof.
I will learn about this one: Object.entries(msg.payload).forEach (arr =>, seems to be very handy for jsons. Thanks a lot.

Yes, it works the way I need! I tested it for mixed order of objects too. However I will stick to @E1cid solution as it saves nodes and some handwork a little. :slight_smile: But thanks, good solution too, I will keep it in mind. I will also learn a bit more about node.send method because I came across that too but did not have experience with that yet. Sort on topics way can be very handy too.

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