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.
asked Mar 1, 2013 at 12:24
spaniard89
3071 gold badge6 silver badges18 bronze badges
2 Answers 2
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;
answered Mar 1, 2013 at 14:25
user568109
48.1k18 gold badges101 silver badges124 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Mar 1, 2013 at 12:27
John Zwinck
252k44 gold badges347 silver badges459 bronze badges
Comments
lang-js