Get specific observations from api.wunderground.com/api/history

99 Views Asked by At

I'm trying to grab a few values from the observation array within the In the history api from api.wunderground.com/api.

http://api.wunderground.com/weather/api/d/docs?d=data/history

I'm a novice with JavaScript but attempting to pull this data into Google Sheet using the json api.

I'm looking for help to get the temp and precip for various observations from yesterday's history. Special credit if you have creative way to get 6 (or every fourth) observations from 6:00am to 7:00pm. I'm just trying to get a random set from the daytime.

    // Fetch Wunderground Weather from Yesterday

  var urlwx = "http://api.wunderground.com/api/" + api + "/history_" + yesterday + "/q/" + country + "/" + city + ".json" ;
  var wxresponse = UrlFetchApp.fetch(urlwx);

Logger.log(urlwx)

// Parse the JSON reply
  var wxcontentText = wxresponse.getContentText();
  var historywx = JSON.parse(wxcontentText);

** //Get Temps this is where I'm stuck **  
  var yesterdayMaxTemp = historywx.history.observations[0].tempi;
  var yesterdayPrecip = historywx.history.observations[0].precipi ;
1

There are 1 best solutions below

0
p0werenner On

Looks like this was working - I'm still open to ideas on how to improve getting temps over multiple observations. For now, I grabbed every fifth observation and declared a variable for each. I'm sure there's a better way and I'm open to suggestions.

//Get Temps from Observations 20, 25, 30, 35, 40, 45, 50, 55  
  var yesterdayMaxTemp1 = historywx.history.observations[20].tempi;
  var yesterdayPrecip1 = historywx.history.observations[20].precipi ;
  var yesterdayMaxTemp2 = historywx.history.observations[25].tempi;
  var yesterdayPrecip2 = historywx.history.observations[25].precipi ;
  var yesterdayMaxTemp3 = historywx.history.observations[30].tempi;
  var yesterdayPrecip3 = historywx.history.observations[30].precipi ;
  var yesterdayMaxTemp4 = historywx.history.observations[35].tempi;
  var yesterdayPrecip4 = historywx.history.observations[35].precipi ;
  var yesterdayMaxTemp5 = historywx.history.observations[40].tempi;
  var yesterdayPrecip5 = historywx.history.observations[40].precipi ;
  var yesterdayMaxTemp6 = historywx.history.observations[45].tempi;
  var yesterdayPrecip6 = historywx.history.observations[45].precipi ;
  var yesterdayMaxTemp7 = historywx.history.observations[50].tempi;
  var yesterdayPrecip7 = historywx.history.observations[50].precipi ;
  var yesterdayMaxTemp8 = historywx.history.observations[55].tempi;
  var yesterdayPrecip8 = historywx.history.observations[55].precipi ;