Solved: Filter data from JSON

I would like to filter certain objects from a large json array
The jsons look basically like this
{"time":1612819223000,"warnings":{"101001000":[{"regionName":"Stadt Flensburg","end":1612....
and I would create a new json with objects that have only a certain regionName, lets say "Stadt Flensburg"

so the function code I am using is

msg.payload = msg.payload.warnings.filter(rg => rg.regionName == "Stadt Flensburg");
return msg;

But I am getting
"TypeError: Cannot read property 'filter' of undefined"

What do I do wrong?

msg.payload.warnings is an object as far as i can see.
msg.payload.warnings["101001000"] is an array, again not totally sure as you only supplied a minor glimpse of the json
filter only works on arrays
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

sorry, here is more:
{"time":1612819223000,"warnings":{"101001000":[{"regionName":"Stadt Flensburg","end":1612868400000,"start":1612798200000,"type":6,"state":"Schleswig-Holstein","level":2,"description":"Es muss mit Glätte durch geringfügigen Schneefall gerechnet werden.","event":"GLÄTTE","headline":"Amtliche WARNUNG vor GLÄTTE","instruction":"","stateShort":"SH","altitudeStart":null,"altitudeEnd":null},{"regionName":"Stadt Flensburg","end":1....
I see your point that filter works only for arrays.
So how can I extract the arrays that report about my target regionName?
The issue is that it can be up to 6 arrays (= warnings). Or zero.
Basically it would be great if I could search the JSON and only extract the targeted warnings, if they exist at all. Of those, I am only interesting in the description.

My current flow is like this, but I would like to have all descriptions in one output

[{"id":"2c9ebf55.8d1628","type":"debug","z":"92c60106.ca09f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":500,"y":1360,"wires":[]},{"id":"9eca9c1a.3847f8","type":"json","z":"92c60106.ca09f8","name":"DWD JSON","property":"payload","action":"","pretty":true,"x":510,"y":1600,"wires":[["41f5adc1.6010dc","254fe078.b9d7b","a10af3d6.d9b438","bc4910be.f57b1","c5e86b54.2d524","7120e130.518da8","f0ee7dc5.2f7cc8","2c9ebf55.8d1628"]]},{"id":"41f5adc1.6010dc","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.0.description","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":1660,"wires":[]},{"id":"254fe078.b9d7b","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.1.description","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":1700,"wires":[]},{"id":"a10af3d6.d9b438","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.2.description","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":1740,"wires":[]},{"id":"bc4910be.f57b1","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.3.description","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":1800,"wires":[]},{"id":"c5e86b54.2d524","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.4.description","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":1840,"wires":[]},{"id":"7120e130.518da8","type":"debug","z":"92c60106.ca09f8","name":"Mettmann Warnung 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.warnings.105158000.5.description","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":1900,"wires":[]},{"id":"f0ee7dc5.2f7cc8","type":"function","z":"92c60106.ca09f8","name":"","func":"const data = [\n  {\n    name: 'Bob',\n    gender: 'male',\n    age: 34,\n  },\n  {\n    name: 'Carol',\n    gender: 'female',\n    age: 36,\n  },\n  {\n    name: 'Ted',\n    gender: 'male',\n    age: 38,\n  },\n  {\n    name: 'Alice',\n    gender: 'female',\n    age: 40,\n  },\n];\n\nmsg.payload= data.filter(e => e.age > 37);\nmsg.payload = msg.payload.warnings.filter(rg => rg.regionName == \"Kreis Mettmann\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":1580,"wires":[["b8e965bd.965ea8"]]},{"id":"b4f4a662.8dff28","type":"function","z":"92c60106.ca09f8","name":"JSONP2JSON","func":"var jsonpDWD = msg.payload;\nvar jsonpDWD2;\njsonpDWD2 = jsonpDWD.substring(24,);\nmsg.payload = jsonpDWD2.substring(0,jsonpDWD2.length - 2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":1480,"wires":[["9eca9c1a.3847f8"]]},{"id":"b8e965bd.965ea8","type":"debug","z":"92c60106.ca09f8","name":"DWD Mettmann","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":1600,"wires":[]},{"id":"44cd37e0.240368","type":"http request","z":"92c60106.ca09f8","name":"DWD JSONP","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json","tls":"","persist":false,"proxy":"","authType":"","x":220,"y":1340,"wires":[["b4f4a662.8dff28"]]},{"id":"c19f73e.d53a81","type":"inject","z":"92c60106.ca09f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1280,"wires":[["44cd37e0.240368"]]}]

target the array

msg.payload = msg.payload.warnings["101001000"].filter(rg => rg.regionName == "Stadt Flensburg");

Thanks @E1cid , this works!

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