Webhook json issue

Hi Everyone,
first off let me say that I am very new at this. I’ve searched the forums, googled, youtubed, read docs, etc etc but I’m still stuck.

So basically I want my photon to get air pollution data and then act on it. The problem is the API that I am using sends me back an array with no name. I can’t figure out how to access the JSON data using the custom webhook template integration.

I’m getting the data from, https://docs.airnowapi.org/.

Here is what is returned…
[{“DateObserved”:"2018-01-16 ",“HourObserved”:20,“LocalTimeZone”:“PST”,“ReportingArea”:“Redwood City”,“StateCode”:“CA”,“Latitude”:37.48,“Longitude”:-122.22,“ParameterName”:“O3”,“AQI”:0,“Category”:{“Number”:1,“Name”:“Good”}},{“DateObserved”:"2018-01-16 ",“HourObserved”:20,“LocalTimeZone”:“PST”,“ReportingArea”:“Redwood City”,“StateCode”:“CA”,“Latitude”:37.48,“Longitude”:-122.22,“ParameterName”:“PM2.5”,“AQI”:54,“Category”:{“Number”:2,“Name”:“Moderate”}}]

You will noticed the above is an unamed array so I can’t do the usual mustache thing of {{a.AQI}} (to get air quality index in the “a” array) to get what i want. Or maybe I can but I’m just doing it wrong.

I’ve been using this as a reference, https://docs.particle.io/reference/webhooks/#variable-substitution and this, https://core-electronics.com.au/tutorials/webhooks-with-particle-io.html , oh and this http://mustache.github.io/mustache.5.html .

Any help would be hugely appreciated.

thanks,
Ethan

Have you tried {{0.AQI}}, {{1.AQI}}?

ScruffR that worked! Thank you. I also received a reply from a friend of mine. I’ve pasted it below as it explains what ScruffR magically did.

“xpath (the standard syntax for walking data structures) works the same was as stuff in c, c++, javascript, etc.
Accessing a named member (e.g. “DateObserved”) is with a “.” (e.g. foo.DateObserved)
Accessing an array element is with [] (e.g. foo[0] gets the first item in the array, foo[1] gets the second item, etc).
So getting to the number and name of the first item in this data would be (assuming “root” is the name of the root) “root[0].Category.Number” (will return 1) and “root[0].Category.Name” (will return “Good”)
If your root is the array itself, try “[0].Category.Number” or “this[0].Category.Number”.”

1 Like

@mckenzethan

you may find this tool by @rickkas7 quite helpful.

1 Like