0

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.

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.