Filter out data from a (changing) array

Hi,

I have an Array that refreshes every 60 seconds, the array looks like this picture

I know i can grab a specific postion using the change node and "payload[0].VR[0]" for example, the problem is that the data in the array seems to be randomly ordered. If i want to read the data (VR) with the ID of "ST: "158", how would i go about this?

Here is the array

[{"ID":"1","TS":"1675867974","ST":"157","VR":[0]},{"ID":"1","TS":"1675867974","ST":"158","VR":[0]},{"ID":"1","TS":"1675867974","ST":"159","VR":[0]},{"ID":"1","TS":"1675867974","ST":"163","VR":[0]},{"ID":"1","TS":"1675867974","ST":"223","VR":[0]},{"ID":"1","TS":"1675867974","ST":"224","VR":[0]},{"ID":"1","TS":"1675867974","ST":"231","VR":[0]},{"ID":"1","TS":"1675867974","ST":"337","VR":[0]},{"ID":"1","TS":"1675867974","ST":"1","VR":[55]},{"ID":"1","TS":"1675867974","ST":"26","VR":[415]},{"ID":"1","TS":"1675867974","ST":"32","VR":[419]},{"ID":"1","TS":"1675867974","ST":"28","VR":[360]},{"ID":"1","TS":"1675867974","ST":"98","VR":[556]},{"ID":"1","TS":"1675867974","ST":"106","VR":[570]},{"ID":"1","TS":"1675867974","ST":"103","VR":[516]},{"ID":"1","TS":"1675867974","ST":"195","VR":[57]},{"ID":"1","TS":"1675867974","ST":"196","VR":[7600]},{"ID":"1","TS":"1675867974","ST":"197","VR":[0]},{"ID":"1","TS":"1675867974","ST":"34","VR":[456]},{"ID":"1","TS":"1675867974","ST":"107","VR":[133]},{"ID":"1","TS":"1675867974","ST":"246","VR":[0]},{"ID":"1","TS":"1675867974","ST":"286","VR":[372]},{"ID":"1","TS":"1675867974","ST":"243","VR":[0]},{"ID":"1","TS":"1675867974","ST":"244","VR":[0]}]

Thanks for your help :slightly_smiling_face:

Hello @TommyTommy,

Here is one way to do it

[{"id":"7b933e12b9945c08","type":"inject","z":"ac3483be8df714b3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"157\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"158\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"159\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"163\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"223\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"224\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"231\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"337\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"1\",\"VR\":[55]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"26\",\"VR\":[415]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"32\",\"VR\":[419]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"28\",\"VR\":[360]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"98\",\"VR\":[556]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"106\",\"VR\":[570]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"103\",\"VR\":[516]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"195\",\"VR\":[57]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"196\",\"VR\":[7600]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"197\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"34\",\"VR\":[456]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"107\",\"VR\":[133]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"246\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"286\",\"VR\":[372]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"243\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"244\",\"VR\":[0]}]","payloadType":"jsonata","x":470,"y":100,"wires":[["1596ed850731f543"]]},{"id":"1bb7db14a735b5b5","type":"debug","z":"ac3483be8df714b3","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":920,"y":100,"wires":[]},{"id":"1596ed850731f543","type":"function","z":"ac3483be8df714b3","name":"function 1","func":"let newmsg = {}\nfor (let i=0; i<msg.payload.length; i++) {\n    newmsg.payload = msg.payload[i]\n    node.send(newmsg)\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":100,"wires":[["719f30f7cda9d490"]]},{"id":"719f30f7cda9d490","type":"switch","z":"ac3483be8df714b3","name":"","property":"payload.ST","propertyType":"msg","rules":[{"t":"eq","v":"158","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":770,"y":100,"wires":[["1bb7db14a735b5b5"]]}]

I have moved the check for ST == 158 out of the function into a switch node, but you could test it inside the function node for loop.

1 Like

You can also do it in a change node with JSONata
here are some examples.

[{"id":"9c588cf1cc587faa","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"158","payload":"[{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"157\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"158\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"159\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"163\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"223\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"224\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"231\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"337\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"1\",\"VR\":[55]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"26\",\"VR\":[415]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"32\",\"VR\":[419]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"28\",\"VR\":[360]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"98\",\"VR\":[556]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"106\",\"VR\":[570]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"103\",\"VR\":[516]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"195\",\"VR\":[57]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"196\",\"VR\":[7600]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"197\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"34\",\"VR\":[456]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"107\",\"VR\":[133]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"246\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"286\",\"VR\":[372]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"243\",\"VR\":[0]},{\"ID\":\"1\",\"TS\":\"1675867974\",\"ST\":\"244\",\"VR\":[0]}]","payloadType":"json","x":110,"y":4800,"wires":[["f97fee70ff26c1fd","ceda5fbce2e10e3a","8c828c73f394245e"]]},{"id":"f97fee70ff26c1fd","type":"change","z":"da8a6ef0b3c9a5c8","name":"return object","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[ST =\"158\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":4760,"wires":[["1f51ffb244896559"]]},{"id":"ceda5fbce2e10e3a","type":"change","z":"da8a6ef0b3c9a5c8","name":"return VR[0]","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[ST =\"158\"].VR[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":4820,"wires":[["4df95968b2f05ab2"]]},{"id":"8c828c73f394245e","type":"change","z":"da8a6ef0b3c9a5c8","name":"Search with topic","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[ST =$$.topic]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":4880,"wires":[["5baa8e74f31659e2"]]},{"id":"1f51ffb244896559","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 231","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":4760,"wires":[]},{"id":"4df95968b2f05ab2","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 232","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":4820,"wires":[]},{"id":"5baa8e74f31659e2","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 233","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":4880,"wires":[]}]

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