Help on extracting values from json output


#1

Hello,

quite a bit newbie to node-red.
Done my forst workflows now but need help for this one:
Flow:
[{"id":"7c222748.416158","type":"tab","label":"Pollen","disabled":false,"info":""},{"id":"20c3a31e.daea2c","type":"inject","z":"7c222748.416158","name":"Get DWD Pollen data","topic":"","payload":"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json","payloadType":"str","repeat":"360","crontab":"","once":true,"onceDelay":0.1,"x":120,"y":40,"wires":[["be1fc1c6.3eb8"]]},{"id":"be1fc1c6.3eb8","type":"http request","z":"7c222748.416158","name":"Get http data","method":"GET","ret":"txt","paytoqs":false,"url":"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json","tls":"","proxy":"","x":330,"y":40,"wires":[["33ba2eed.e1cc52"]]},{"id":"fb2f0ecb.eb179","type":"debug","z":"7c222748.416158","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":180,"wires":[]},{"id":"33ba2eed.e1cc52","type":"json","z":"7c222748.416158","name":"","property":"payload","action":"","pretty":true,"x":500,"y":40,"wires":[["fb2f0ecb.eb179"]]}]

In the debug output window, i see the output as following:

Need to extract the value from the 22nd array (with id 110) and want to have the values from the object named Pollen -> Ambrosia -> today.

The same for tomorrow and several others.
I want to store those into my Mosquitto DB to show within HA.

BTW. running latest HA release 89.2 on hass.io (Node-Red add-on).

Thanks for your help in advance!
/ Ralf


#2

You can use a function node to iterate the content array, the typical javascript for loop

    for (var i = 0; i < msg.payload.content.length; i++) {
        node.log(msg.payload.content[i])
    }

or also use the iterate node with a switch node, more the nodered way

[{"id":"eaee7329.9313f","type":"inject","z":"ef925df2.7198","name":"Get DWD Pollen data","topic":"","payload":"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":200,"wires":[["e53c5917.b85a28"]]},{"id":"e53c5917.b85a28","type":"http request","z":"ef925df2.7198","name":"Get http data","method":"GET","ret":"txt","url":"https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json","tls":"","x":510,"y":200,"wires":[["c8f5b6d2.457958"]]},{"id":"c8f5b6d2.457958","type":"json","z":"ef925df2.7198","name":"","property":"payload","action":"","pretty":true,"x":680,"y":200,"wires":[["45d97c1f.574d84"]]},{"id":"45d97c1f.574d84","type":"Serial Iterator","z":"ef925df2.7198","name":"","property":"payload.content","inputFlow":"input","saveOutput":0,"recursive":0,"storeId":0,"x":850,"y":200,"wires":[["97a22e39.cf42d"],[]]},{"id":"13e5dbd3.52b9a4","type":"debug","z":"ef925df2.7198","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1010,"y":280,"wires":[]},{"id":"97a22e39.cf42d","type":"switch","z":"ef925df2.7198","name":"","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"object","vt":"object"},{"t":"jsonata_exp","v":"payload.partregion_id = 124","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":280,"wires":[["45d97c1f.574d84"],["13e5dbd3.52b9a4"]]}]


#3

@subzero79: Thanks for the reply, will try your solution.

/ Ralf