How will I parse the following JSON in node.js to extract the value of temp and city
{
"message":"",
"cod":"200",
"type":"base",
"calctime":"",
"units":"internal",
"count":1,
"list":
[
{"id":2823368,
"coord":{"lat":47.666672,"lon":9.6},
"name":"London",
"main":{"temp":275.79,"pressure":1020,"humidity":74,"temp_min":272.59,"temp_max":281.48},
"dt":1362137169,
"date":"2013-03-01 11:26:09",
"wind":{"speed":1.5,"deg":0},
"clouds":{"all":90},
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
"sys":{"country":"DE","population":18135},
"url":"http:\/\/openweathermap.org\/city\/2823368"
}
]
}
I get the above JSON by:
var response = JSON.parse(body);
console.log(response);
Any help would be really appreciated.
Use following to get temp and city (city from url) or else use name
var temp = response.list[0].main.temp,
url = response.list[0].url,
city = url.split('/')[3],
name = response.list[0].name;
var temp = response.list[0].main.temp;
var city = response.list[0].name;
As for "city" I'm not sure what you're looking for since there's no key by that name in your input, but I took a guess.