JSON Filtering ID and plot the Values

I would like to Filter this json file and plot the values from the ID "15239299475" and "15239299476".

I read the file in:

And this is how the msg.payload looks like:

I need some help with the function box.

Thank you for your help!

This is the json file:
https://api.luftdaten.info/static/v2/data.dust.min.json

Leaving aside the fact that I don't see id 15239299475 or 15239299476 amongst the 5+MB of your sample data, possibly because it blew my poor Raspberry Pi Zero's mind...
You haven't said if you expect just one or many occurrences of your chosen ids in each input batch.

Maybe something like this would work:

msg.payload.forEach(myFunction);

function myFunction(item) {
  item.sensordatavalues.forEach(anotherFunction);
}

function anotherFunction(anotheritem) {
    if ((anotheritem.id == 15239299475) || (anotheritem.id == 15239299476)) {
    msg.payload = anotheritem;
    node.send(msg);
    }
}

But if I had to handle such a large volume of data I'd probably use Linux commands to prefilter it.
First split it into new lines at }]},
Then grep for the lines of interest.

ps JSonata might be able to filter out the required elements, I'm not familiar with it's abilities.

Thank you for you answer. I tried something with the JSONata.

Now I have this Output:

I have a lot of Objects. How can I for example only plot the Object with the id: xy?
The Object are not always at the same position in the array.

It looks like this now:

Thank you!

You can filter by adding [id = 152392299475] to the end of your expression. Or after payload and before the .

[edit]Thinking on it you want the values to plot in a chart

$$.payload.sensordatavalues[$.id in [15251545897,15251545902]].${value_type:value}

returns

{
  "P1":"20.01",
  "P2":"17.77"
}

Thank you! :slight_smile: It works :100:

Hi Gerry, glad you got a solution.

JSONata is great for many things however with the size of that data, be aware, you will see significant processing time compared to straight JavaScript...

JavaScript: 5ms
JSONata: 1588ms

If this additional processing time is not an issue then feel free to ignore this :slight_smile:

Demo flow...

[{"id":"9a693308.7ebaf","type":"subflow","name":"flow-timer","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"7fc82258.93e36c"}]}],"out":[{"x":440,"y":100,"wires":[{"id":"7fc82258.93e36c","port":0}]}],"env":[{"name":"name","type":"str","value":"measure","ui":{"icon":"font-awesome/fa-tag","label":{"en-US":"Timer Name"},"type":"input","opts":{"types":["str","env"]}}},{"name":"operation","type":"str","value":"start","ui":{"icon":"font-awesome/fa-cog","label":{"en-US":"Operation"},"type":"select","opts":{"opts":[{"l":{"en-US":"start"},"v":"start"},{"l":{"en-US":"stop"},"v":"stop"},{"l":{"en-US":"msg.topic"},"v":"msg.topic"},{"l":{"en-US":"msg.operation"},"v":"msg.operation"},{"l":{"en-US":"msg.payload"},"v":"msg.payload"}]}}}],"meta":{"module":"node-red-contrib-flow-performance","type":"flow-performance","version":"1.0.1","author":"Steve-Mcl","desc":"Inline flow performance measure node","keywords":"node-red performance","license":"MIT"},"color":"#DAEAAA","icon":"node-red/timer.svg","status":{"x":280,"y":160,"wires":[{"id":"7fc82258.93e36c","port":1}]}},{"id":"7fc82258.93e36c","type":"function","z":"9a693308.7ebaf","name":"do operation","func":"// @ts-ignore\nvar name = msg.perfName || env.get(\"name\");\n// @ts-ignore\nvar operation = msg.perfOperation || env.get(\"operation\");\nvar measures = global.get(\"flow_timers\") || {};\nvar measure = measures[name] || {};\n\nfunction doOp(measure, op){\n    if(operation === \"start\"){\n        measure.start = Date.now();//change to process.hrtime\n        measure.stop = null;\n        measure.durationMs = null;\n    } else if(operation === \"stop\") {\n        measure.stop = Date.now();//change to process.hrtime\n        measure.durationMs = measure.start ? measure.stop - measure.start : null;\n        msg._performance = measure;\n    }\n}\n\n\nif(operation === \"start\"){\n    doOp(measure, operation);\n} else if(operation === \"stop\") {\n    doOp(measure, operation);\n    node.send([null, { payload: { text: name + \": \" + measure.durationMs + \"ms\" }}]);\n} else if(operation === \"msg.topic\") {\n    operation = msg.topic;\n    doOp(measure, operation);\n} else if(operation === \"msg.operation\") {\n    operation = msg.operation;\n    doOp(measure, operation);\n} else if(operation === \"msg.payload\") {\n    operation = msg.payload;\n    doOp(measure, operation);\n} else {\n    return [msg, null];\n}\nmeasures[name] = measure;\nglobal.set(\"flow_timers\", measures);\n\nreturn [msg, null];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":100,"wires":[[],[]]},{"id":"1ae72b293183ea8a","type":"inject","z":"af952aeaa20f4f97","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1500,"y":300,"wires":[["6e69f5002c8ad033"]]},{"id":"6e69f5002c8ad033","type":"http request","z":"af952aeaa20f4f97","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://api.luftdaten.info/static/v2/data.dust.min.json","tls":"","persist":false,"proxy":"","authType":"","x":1670,"y":300,"wires":[["dcf2270d277cb178"]]},{"id":"dcf2270d277cb178","type":"subflow:9a693308.7ebaf","z":"af952aeaa20f4f97","name":"","env":[{"name":"name","value":"jsonata","type":"str"}],"x":1820,"y":300,"wires":[["4a1d56279c8d0b40"]]},{"id":"188cd4f741a68b28","type":"subflow:9a693308.7ebaf","z":"af952aeaa20f4f97","name":"","env":[{"name":"name","value":"jsonata","type":"str"},{"name":"operation","value":"stop","type":"str"}],"x":2200,"y":300,"wires":[["8b7afb73c0661af1"]]},{"id":"8b7afb73c0661af1","type":"debug","z":"af952aeaa20f4f97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2370,"y":300,"wires":[]},{"id":"4a1d56279c8d0b40","type":"change","z":"af952aeaa20f4f97","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[id=6953642909].{\t    \"id\": id,\t    \"value\": sensordatavalues.value,\t    \"value_type\": sensordatavalues.value_type\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2000,"y":300,"wires":[["188cd4f741a68b28"]]},{"id":"7ec2b9f135fc389e","type":"inject","z":"af952aeaa20f4f97","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1500,"y":220,"wires":[["096632d22f17bf53"]]},{"id":"096632d22f17bf53","type":"http request","z":"af952aeaa20f4f97","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://api.luftdaten.info/static/v2/data.dust.min.json","tls":"","persist":false,"proxy":"","authType":"","x":1670,"y":220,"wires":[["8bc7d565e76cf1f5"]]},{"id":"8bc7d565e76cf1f5","type":"subflow:9a693308.7ebaf","z":"af952aeaa20f4f97","name":"","env":[{"name":"name","value":"javascript","type":"str"}],"x":1820,"y":220,"wires":[["aafc3128309b396e"]]},{"id":"eeab09ee6f2c3477","type":"subflow:9a693308.7ebaf","z":"af952aeaa20f4f97","name":"","env":[{"name":"name","value":"javascript","type":"str"},{"name":"operation","value":"stop","type":"str"}],"x":2200,"y":220,"wires":[["eea4bd31d3443381"]]},{"id":"eea4bd31d3443381","type":"debug","z":"af952aeaa20f4f97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2370,"y":220,"wires":[]},{"id":"aafc3128309b396e","type":"function","z":"af952aeaa20f4f97","name":"","func":"const result = msg.payload.find(e => e.id == 6953642909)\n\nif (result) {\n    msg.payload = {\n        id: result.id,\n        sensordatavalues: result.sensordatavalues\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1980,"y":220,"wires":[["eeab09ee6f2c3477"]]}]
1 Like

Wow thats amazing!
How do you write this JSONata in js?

payload[sensor.id = 57603].{
"id" : sensor.id,
"PM10 and PM2.5" : sensordatavalues.value
}