I am trying to parse this JSON file. I was able to get temp easy but I am not able to get main from the weather. I assume this is due to the “weather”: [ than normal JSON file.
Does anyone know how to parse this correctly to be able to access the weather.main data.
JSON file:
{
"coord": {
"lon": -4.5063,
"lat": 55.6787
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02n"
}
],
"base": "stations",
"main": {
"temp": 3.72,
"feels_like": 1.32,
"temp_min": 3.07,
"temp_max": 4.94,
"pressure": 1024,
"humidity": 75
}
Here is my current parser:
async function getWeather(lat, lon) {
const params = new URLSearchParams({
lat,
lon,
units: "metric",
appid: API_KEY,
})
return fetch(`https://api.openweathermap.org/data/2.5/weather?${params}`)
.then(function (response) {
return response.json()
}).then(function (obj) {
console.log(obj.main.temp)
weatherTemp = obj.main.temp;
console.log("OBJ" + obj.weather.main) // does not work, return undefined. Not able to access
//weatherType = obj.weather.main;
// print();
// ContextMerge();
}).catch(function (error) {
console.log("Something went wrong" + error)
});
}