How to separate information from message payload

Hi All,

Please help me how to filter information from this payload, i just want only read the "Value" from each "ID".

[{"errorCode":"1001","id":"103052.0.184679"},{"id":"103052.0.184677","value":"50.0"},{"id":"103052.0.184678","value":"1"},{"errorCode":"1003","id":"103053.0.184674"},{"errorCode":"1003","id":"103053.0.184675"},{"id":"103052.0.184676","value":"23.1"},{"id":"103052.0.184683","value":"1079"},{"id":"103052.0.184682","value":"924.5"},{"id":"103052.0.184681","value":"365.4"},{"id":"103052.0.184680","value":"67"},{"id":"103050.0.184686","value":"0"},{"errorCode":"1001","id":"103052.0.184685"},{"id":"103052.0.184684","value":"50.0"}]

function node:

m = msg.payload //<-array input

m.forEach(function (i) {
    if(i.hasOwnProperty("value")){
        node.send({"payload":{"id":i.id,"value":i.value}})
    }
});

1 Like

Hi Bakman2,

I've test your function, but i got message error:

TypeError: m.forEach is not a function

Do you just need a list of the values or do you need both the id and value? I.e. break that msg.pauload into a bunch of msgs?

1 Like

Hi zenofmud,

This ID is a measuring parameters, and Value is data of each ID. So, i need break the payload to identify the value of each ID, Sir.

1 Like

Hi Guys,

Finally i can split the payload. Here's the pic of my node. Thank you all

That is strange, works just fine.

Try this flow, instead of 10 function nodes, it can be done with 1 - and converting to json is not required (unless your input data is not json)

[{"id":"b6861925.713f38","type":"function","z":"5411318a.314a48","name":"","func":"m = msg.payload //<-array input\nout = [];\n\nm.forEach(function (i) {\n    if(i.hasOwnProperty(\"value\")){\n        out.push([{\"payload\":{\"id\":i.id,\"value\":i.value}}])\n    }\n});\n\nreturn out;\n\n\n","outputs":9,"noerr":0,"x":510,"y":460,"wires":[["a7f75738.62a5f8"],["34c1f61b.ef6c0a"],["2fa0c42d.093a0c"],["29fe2863.c872b"],["7adf5a19.5612e4"],["c2dbb34.7b1125"],["53271747.dff598"],["e9f39150.59437"],["1f9a09cf.91c376"]]},{"id":"f7364f32.aa6108","type":"inject","z":"5411318a.314a48","name":"","topic":"","payload":"[{\"errorCode\":\"1001\",\"id\":\"103052.0.184679\"},{\"id\":\"103052.0.184677\",\"value\":\"50.0\"},{\"id\":\"103052.0.184678\",\"value\":\"1\"},{\"errorCode\":\"1003\",\"id\":\"103053.0.184674\"},{\"errorCode\":\"1003\",\"id\":\"103053.0.184675\"},{\"id\":\"103052.0.184676\",\"value\":\"23.1\"},{\"id\":\"103052.0.184683\",\"value\":\"1079\"},{\"id\":\"103052.0.184682\",\"value\":\"924.5\"},{\"id\":\"103052.0.184681\",\"value\":\"365.4\"},{\"id\":\"103052.0.184680\",\"value\":\"67\"},{\"id\":\"103050.0.184686\",\"value\":\"0\"},{\"errorCode\":\"1001\",\"id\":\"103052.0.184685\"},{\"id\":\"103052.0.184684\",\"value\":\"50.0\"}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":460,"wires":[["b6861925.713f38"]]},{"id":"a7f75738.62a5f8","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":300,"wires":[]},{"id":"34c1f61b.ef6c0a","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":340,"wires":[]},{"id":"2fa0c42d.093a0c","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":380,"wires":[]},{"id":"29fe2863.c872b","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":420,"wires":[]},{"id":"1f9a09cf.91c376","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":620,"wires":[]},{"id":"e9f39150.59437","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":580,"wires":[]},{"id":"53271747.dff598","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":540,"wires":[]},{"id":"7adf5a19.5612e4","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":460,"wires":[]},{"id":"c2dbb34.7b1125","type":"debug","z":"5411318a.314a48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":500,"wires":[]}]

My fault not inform detail for the input. The input is JSON from http request. Your code is working fine after i add JSON. Thanks bakman

Hi, @bakman2 first suggestion should now work & it may be a better solution as it is dynamic.

What I mean is if there are 9 or 1 or a 1000 items, it will still work.

Replace your multi output function with a single output function and use his code...