Decoding JSON

Hey gang. I’m new to Imp and new to JSON.
I’ve made a lot of progress from code I’ve been able to reference from the forums. (Thank you, community!)

I’m working with the forcast.io api and I feel like I’m taking crazy pills trying to extract some data.

Here’s the forecast.io response:

{“latitude”:33.988687,“longitude”:-84.508454,“timezone”:“America/New_York”,“offset”:-5,“currently”:{“time”:1390150800,“summary”:“Clear”,“icon”:“clear-day”,“precipIntensity”:0,“precipProbability”:0,“temperature”:48.03,“apparentTemperature”:42.73,“dewPoint”:26.59,“humidity”:0.43,“windSpeed”:12.72,“windBearing”:290,“visibility”:10,“cloudCover”:0,“pressure”:1018.8},“daily”:{“data”:[{“time”:1390107600,“summary”:“Clear throughout the day.”,“icon”:“clear-day”,“sunriseTime”:1390135394,“sunsetTime”:1390172193,“moonPhase”:0.63,“precipIntensity”:0,“precipIntensityMax”:0,“precipProbability”:0,“temperatureMin”:33.71,“temperatureMinTime”:1390136400,“temperatureMax”:51.73,“temperatureMaxTime”:1390176000,“apparentTemperatureMin”:29.16,“apparentTemperatureMinTime”:1390136400,“apparentTemperatureMax”:51.73,“apparentTemperatureMaxTime”:1390176000,“dewPoint”:23.24,“humidity”:0.47,“windSpeed”:7.21,“windBearing”:260,“visibility”:10,“cloudCover”:0,“pressure”:1016.98}]},“flags”:{“sources”:[“isd”],“isd-stations”:[“722156-53873”,“722195-03888”,“722196-53863”,“722270-13864”,“747812-63813”],“units”:“us”}}

I want the data in the “daily” section at the bottom of the response. Specifically the sunriseTime, sunsetTime, and precipProbability.

Any suggestions?

Have you looked at this yet?
http://electricimp.com/docs/api/http/jsondecode/

I’m just sort of guessing on this part … something like this?

`
// create a request handler
http.onrequest(function(request, response){
try
{
// decode the json - if it’s invalid json an error will be thrown
local data = http.jsondecode(request.body);

// check if particular keys are in the json body
if (“sunriseTime” in data) {
// send message to device
device.send(“sunriseTime”, data.sunriseTime);
}
if (“sunsetTime” in data) {
// send message to device
device.send(“sunsetTime”, data.sunsetTime);
}
// send response to whoever hit the agent url
response.send(200, “OK”);
}
catch (e) {
// send a response indicating invalid JSON
response.send(500, “Invalid JSON string”);
}
});
`

Thanks mlseim. That might work but I want to do more with the data on the agent side before tickling the device.

Here’s where I’m at:

// hand off data to be parsed local response = http.jsondecode(res.body); // load table "forecast" with the JSON data local forecast = response.daily.data; // get Sunrise time string local uSunrise = ""; uSunrise += (forecast.sunriseTime); server.log("Sunrise: " + uSunrise); // get Sunset time string local uSunset = ""; uSunset += (forecast.sunsetTime); server.log("Sunset: " + uSunset); // get precipIntensityMax local precipIntensity = ""; precipIntensity += (forecast.precipIntensityMax); server.log("PrecipIntensity " + precipIntensity);

In the device log, I get a notice “the index ‘sunriseTime’ does not exist”

Edit: Added < code > </ code > blocks to code to make more legible.

response.daily.data is actually an array (you can tell by the [ ]). You’ll want to change it to:

local forecast = response.daily.data[0];

:">
My face is red… I should have known that was an array.
For my edification, would I always reference index 0?
Is data a single element array?

You rock beardedinventor! I would expected nothing less from a fellow beardsmen.

I haven’t played with forecast.io a whole lot, so I’m not sure what you should be expecting back? It can be good idea to use the in keyword to check whether the data you’re looking for exists (and do other checks to make sure it looks like what you’re expecting):

`// Get the forecast:
//if forecase is null at the end of this, things were not formatted as expected
local forecast = null;

local response = http.jsondecode(res.body);
if (“daily” in response && “data” in response.data) {
if (response.data.daily.len() > 0) {
forecast = response.data.daily[0].forecast;
}
}`