Skip to content
Advertisement

How do I retrieve and transpose data to a webpage from an API?

I am having trouble accessing data from an API. Here is my code with a fake access code.

$(function () {

    var $data = ('#data');
    $.ajax({
        type: 'GET',
        url: 'http://api.openweathermap.org/data/2.5/weather?lat=47.6062&lon=-122.3321&units=imperial&appid=a0bfe75fbff13d4040af7eb66d1d82b5',
        success: function (data) {
            $.each(data, function (i, item) {
                $data.append('<li>Temp: ' + item.main['temp'] + '</li>');
            });
        }
    });
    
});

I am getting an error that says “Uncaught TypeError: Cannot read property ‘temp’ of undefined” Here is the API.

{
    "coord": {
        "lon": -122.3321,
        "lat": 47.6062
    },
    "weather": [
        {
            "id": 721,
            "main": "Haze",
            "description": "haze",
            "icon": "50d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 61.36,
        "feels_like": 61.2,
        "temp_min": 55.49,
        "temp_max": 66.31,
        "pressure": 1022,
        "humidity": 85
    },
    "visibility": 10000,
    "wind": {
        "speed": 1.01,
        "deg": 319,
        "gust": 5.01
    },
    "clouds": {
        "all": 1
    },
    "dt": 1627137674,
    "sys": {
        "type": 2,
        "id": 2004026,
        "country": "US",
        "sunrise": 1627130239,
        "sunset": 1627185243
    },
    "timezone": -25200,
    "id": 5809844,
    "name": "Seattle",
    "cod": 200
}

I want to be able to access temp and description and append them or add them to my page. It does not need to be a list, but I want to style it in css. Feel free to change any of the code.

{"coord":{"lon":-122.33,"lat":47.61},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":64.09,"feels_like":63.91,"temp_min":57.43,"temp_max":69.62,"pressure":1022,"humidity":79},"visibility":10000,"wind":{"speed":1.99,"deg":325,"gust":3},"clouds":{"all":1},"dt":1627141350,"sys":{"type":2,"id":2004026,"country":"US","sunrise":1627130238,"sunset":1627185243},"timezone":-25200,"id":5809844,"name":"Seattle","cod":200}```

Advertisement

Answer

To access temp you don’t need to use .each loop you can access them directly i.e : data.main.temp and for accessing the weather -> description you can use .each loop and inside each loop use item.description.

Demo Code :

var $data = ('#data');
//dummy json 
var data = {
  "coord": {
    "lon": -122.33,
    "lat": 47.61
  },
  "weather": [{
    "id": 721,
    "main": "Haze",
    "description": "haze",
    "icon": "50d"
  }],
  "base": "stations",
  "main": {
    "temp": 64.09,
    "feels_like": 63.91,
    "temp_min": 57.43,
    "temp_max": 69.62,
    "pressure": 1022,
    "humidity": 79
  },
  "visibility": 10000,
  "wind": {
    "speed": 1.99,
    "deg": 325,
    "gust": 3
  },
  "clouds": {
    "all": 1
  },
  "dt": 1627141350,
  "sys": {
    "type": 2,
    "id": 2004026,
    "country": "US",
    "sunrise": 1627130238,
    "sunset": 1627185243
  },
  "timezone": -25200,
  "id": 5809844,
  "name": "Seattle",
  "cod": 200
}
/* $.ajax({
   //your codes....
   */
console.log(data.main.temp) //use it like this...
$.each(data.weather, function(i, item) {
  console.log(item.description) //get description like this..
})
/*
}
});*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement