Parsing JSON data from wuderground API

99 views
Skip to first unread message

Scott

unread,
Nov 13, 2017, 8:40:50 PM11/13/17
to Jansson users
I'm struggling to figure out how to parse the data from the Wunderground API. I am using the API to get a JSON object for the current observation. The object looks like:
{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
	}
  ,	"current_observation": {
		"image": {
		"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
		"title":"Weather Underground",
		"link":"http://www.wunderground.com"
		},
		"display_location": {
		"full":"Greentown, IN",
		"city":"Greentown",
		"state":"IN",
		"state_name":"Indiana",
		"country":"US",
		"country_iso3166":"US",
		"zip":"46936",
		"magic":"1",
		"wmo":"99999",
		"latitude":"40.47999954",
		"longitude":"-85.97000122",
		"elevation":"256.0"
		},
		"observation_location": {
		"full":"Greentown, IN, Greentown, Indiana",
		"city":"Greentown, IN, Greentown",
		"state":"Indiana",
		"country":"US",
		"country_iso3166":"US",
		"latitude":"40.474854",
		"longitude":"-85.956253",
		"elevation":"846 ft"
		},
		"estimated": {
		},
		"station_id":"KINGREEN32",
		"observation_time":"Last Updated on November 13, 8:35 PM EST",
		"observation_time_rfc822":"Mon, 13 Nov 2017 20:35:13 -0500",
		"observation_epoch":"1510623313",
		"local_time_rfc822":"Mon, 13 Nov 2017 20:39:08 -0500",
		"local_epoch":"1510623548",
		"local_tz_short":"EST",
		"local_tz_long":"America/New_York",
		"local_tz_offset":"-0500",
		"weather":"Mostly Cloudy",
		"temperature_string":"33.4 F (0.8 C)",
		"temp_f":33.4,
		"temp_c":0.8,
		"relative_humidity":"91%",
		"wind_string":"Calm",
		"wind_dir":"WNW",
		"wind_degrees":303,
		"wind_mph":0.0,
		"wind_gust_mph":"5.0",
		"wind_kph":0,
		"wind_gust_kph":"8.0",
		"pressure_mb":"1030",
		"pressure_in":"30.43",
		"pressure_trend":"0",
		"dewpoint_string":"31 F (-0 C)",
		"dewpoint_f":31,
		"dewpoint_c":-0,
		"heat_index_string":"NA",
		"heat_index_f":"NA",
		"heat_index_c":"NA",
		"windchill_string":"33 F (1 C)",
		"windchill_f":"33",
		"windchill_c":"1",
		"feelslike_string":"33 F (1 C)",
		"feelslike_f":"33",
		"feelslike_c":"1",
		"visibility_mi":"2.5",
		"visibility_km":"4.0",
		"solarradiation":"0",
		"UV":"0.0","precip_1hr_string":"0.00 in ( 0 mm)",
		"precip_1hr_in":"0.00",
		"precip_1hr_metric":" 0",
		"precip_today_string":"0.00 in (0 mm)",
		"precip_today_in":"0.00",
		"precip_today_metric":"0",
		"icon":"mostlycloudy",
		"icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
		"forecast_url":"http://www.wunderground.com/US/IN/Greentown.html",
		"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KINGREEN32",
		"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=40.474854,-85.956253",
		"nowcast":""
	}
}




I want to parse out the "temp_f" and "feelslike_f" values. I've tried using json_unpack but it seems to use this I would have to list each element in the function call. 

How can I do this? 

Petri Lehtinen

unread,
Nov 14, 2017, 2:32:13 AM11/14/17
to jansso...@googlegroups.com
Hi!

There's no need to specify all object keys, you can list just what you
need. Assuming root contains the full JSON object, you can do:

double temp_f;
const char *feelslike_f;

json_unpack(root, "{s: {s: f, s: s}}",
"current_observation",
"temp_f", &temp_f,
"feelslike_f", &feelslike_f)

Hope this helps.

Petri
> --
> --
> Jansson users mailing list
> jansso...@googlegroups.com
> http://groups.google.com/group/jansson-users
> ---
> You received this message because you are subscribed to the Google Groups
> "Jansson users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to jansson-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Scott Pagington

unread,
Nov 14, 2017, 6:55:14 PM11/14/17
to jansso...@googlegroups.com
Petri,
That's a big help. Thank you. 

Scott


> For more options, visit https://groups.google.com/d/optout.

You received this message because you are subscribed to a topic in the Google Groups "Jansson users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jansson-users/rpvjuoiaZ1c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jansson-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages