Webhook / json help

Hi,

I’m a newbie to JSON and could use some help. I’m sure it’s pretty basic but I can’t seem to understand how to iterate a JSON array.

The weather query below works great, until I realized the weather “alert” section is some sort of array (my area had 3 weather alerts at the same time). My code just pulls the first event but how do I iterate through the variable sized array? I am just looking to pull the time and expires out of it. I put the snippet of the API return data below that I need to parse.

Right now I just strtok through the returned string.

Thanks for any help!

Mike

JSON:

{
“event”: “forecastio_webhook”,
“url”: “https://api.forecast.io/forecast/MY_API_KEY”,
“requestType”: “GET”,
“headers”: {
“Content-Type”: “application/json”
},
“responseTemplate”: "{{#currently}}{{temperature}}~{{humidity}}~{{pressure}}~{{precipProbability}}~{{windSpeed}}~{{/currently}}{{#alerts.0}}{{time}}~{{expires}}~{{/alerts.0}}",
“query”: “exclude=minutely,hourly,daily,flags”,
“mydevices”: true
}

Alert section of data
"alerts":[{“title”:“Coastal Flood Advisory for Marin, CA”,“time”:1449830760,expires":1449864000,“description”:”…HIGH SURF WARNING NOW IN EFFECT UNTIL 5 PM PST THIS\nAFTERNOON…\n…COASTAL FLOOD ADVISORY REMAINS IN EFFECT FROM 8 AM THIS\nMORNING TO NOON PST TODAY…\n* COASTAL FLOODING…COASTAL FLOODING WILL BE POSSIBLE NEAR\nSAUSALITO DURING HIGH TIDE FRIDAY MORNING.\n* WAVES AND SURF…WESTERLY SWELL AROUND 18 TO 28 FEET WITH\nPERIODS OF 17 SECONDS WILL GRADUALLY DECREASE TO 14 TO 18 FEET\nON FRIDAY.\n* TIMING…SWELL IS PEAKING THIS MORNING AND WILL DECREASE LATER\nTODAY.\n* FLOODING IMPACTS…FLOODING OF LOW LYING AREAS…SUCH AS\nPARKING LOTS AND ROADWAYS NEAR THE BAY.\n* SURF IMPACTS…EXTREME WAVE RUN UP IS EXPECTED WITH THESE\nWAVES WITH LONG LULLS IN BETWEEN WAVE SETS…INCREASING THE\nRISK OF BEING WASHED INTO THE SEA BY A WAVE.\n",“uri”:“http://alerts.weather.gov/cap/wwacapget.php?x=CA1253D1C79958.CoastalFloodAdvisory.1253D1C90E00CA.MTRCFWMTR.c4b5fe31bdaea424f57e27d5c5031e99”},{“title”:“High Surf Warning for San Francisco, CA”,“time”:1449830760,“expires”:1449882000,“description”:"…HIGH SURF WARNING NOW IN EFFECT UNTIL 5 PM PST THIS\nAFTERNOON…\n* WAVES AND SURF…WESTERLY SWELL AROUND 18 TO 28 FEET WITH SWELL\nPERIODS OF 17 SECONDS…THEN GRADUALLY DECREASING TO 14 TO 18\nFEET ON FRIDAY.\n* TIMING…SWELL IS PEAKING THIS MORNING AND WILL DECREASE LATER\nTODAY.\n* IMPACTS…EXTREME WAVE RUN UP IS EXPECTED WITH THESE WAVES\nWITH LONG LULLS IN BETWEEN WAVE SETS…INCREASING THE RISK OF\nBEING WASHED INTO THE SEA BY A WAVE.\n",“uri”:“http://alerts.weather.gov/cap/wwacapget.php?x=CA1253D1C79958.HighSurfWarning.1253D1D56A10CA.MTRCFWMTR.c4b6ba722db4992597e3bb4f9f95dd73"},**{“title”:"High Surf Warning for Marin, CA”,“time”:1449830760,“expires”:1449882000,“description":"…HIGH SURF WARNING NOW IN EFFECT UNTIL 5 PM PST THIS\nAFTERNOON…\n…COASTAL FLOOD ADVISORY REMAINS IN EFFECT FROM 8 AM THIS\nMORNING TO NOON PST TODAY…\n COASTAL FLOODING…COASTAL FLOODING WILL BE POSSIBLE NEAR\nSAUSALITO DURING HIGH TIDE FRIDAY MORNING.\n WAVES AND SURF…WESTERLY SWELL AROUND 18 TO 28 FEET WITH\nPERIODS OF 17 SECONDS WILL GRADUALLY DECREASE TO 14 TO 18 FEET\nON FRIDAY.\n* TIMING…SWELL IS PEAKING THIS MORNING AND WILL DECREASE LATER\nTODAY.\n* FLOODING IMPACTS…FLOODING OF LOW LYING AREAS…SUCH AS\nPARKING LOTS AND ROADWAYS NEAR THE BAY.\n* SURF IMPACTS…EXTREME WAVE RUN UP IS EXPECTED WITH THESE\nWAVES WITH LONG LULLS IN BETWEEN WAVE SETS…INCREASING THE\nRISK OF BEING WASHED INTO THE SEA BY A WAVE.\n”,“uri”:“http://alerts.weather.gov/cap/wwacapget.php?x=CA1253D1C79958.HighSurfWarning.1253D1D56A10CA.MTRCFWMTR.51ac5b6575fab86b14eb1eccc59a5b0a”}]

did you try moustache?

1 Like

It’s not really an answer to your question, but since you strtok through the returned value, why not display the whole array of alerts and let your code pick up the first token only?

That’s my question - how do I iterate via JSON? If I can get all the elements into a string I can handle it from there but just not sure how to get anything other than the first item.

Just use the name of the array property and the block will be called for every array member:

1 Like

Thanks! This is exactly what I needed - I was having syntax issues.