0

I have this async function below that taps into a weather API and I would just like to retrieve two pieces of info from the API: Temperature in F & C. I removed the API_Key but it's free to get one on the site if necessary.

I can confirm I am receiving the json object due to my console.log(response) statement, but Im not sure how to access these data points in such heavily embedded json notation.

I guess my question is if I wanted to access say 'full' for full city name I thought I'd do something like response.observation_location.full but that doesn't work...

Help?

async loadWeather() {
        // let zip = this.args.zip || 97239;
        let response = await fetch(`http://api.wunderground.com/api/API_KEY/conditions/q/CA/San_Francisco.json`);
        console.log(response);
        this.weather = await response.json();
        // setTimeout( () => { this.loadWeather(); }, 2000);
    }

Here is a partial output of the response json:

{
    "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": "San Francisco, CA",
            "city": "San Francisco",
            "state": "CA",
            "state_name": "California",
            "country": "US",
            "country_iso3166": "US",
            "zip": "94102",
            "magic": "1",
            "wmo": "99999",
            "latitude": "37.77999878",
            "longitude": "-122.41999817",
            "elevation": "60.0"
        },
        "observation_location": {
            "full": "SOMA, San Francisco, California",
            "city": "SOMA, San Francisco",
            "state": "California",
            "country": "US",
            "country_iso3166": "US",
            "latitude": "37.778488",
            "longitude": "-122.408005",
            "elevation": "23 ft"
        },

I have tried doing console.log(response["current_observation"]) just to access a nested data value but that doesnt seem to work as it returns undefined.

2
  • What is the response that you are receiving? Commented Jul 5, 2017 at 1:16
  • almost identical to what the example on this page shows wunderground.com/weather/api/d/docs Commented Jul 5, 2017 at 5:11

1 Answer 1

1

Ok I solved my own issue, but for the record:

response needed to be converted to json via resonse.json()

and then I can access properties as expected

this.weather = await response.json();
console.log(this.weather["current_observation"]["temp_f"]);
console.log(this.weather["current_observation"]["temp_c"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.