I'm new to JSON, node.js and javascript... I have a small node.js server that fetch a JSON object(named weatherData below) from openweathermap and I'm trying to make it display only a specific part of the JSON object(a section called "temp"). So far I used:
var tempString = JSON.stringify(weatherData);
And then I used search and substring methods of string to get my info but there's got to be an easier way right? How do I only display the temp section without using string methods?
EDIT: When I stringify the whole thing I get this
"{\"coord\":{\"lon\":-75.7,\"lat\":45.41},\"sys\":{\"type\":3,\"id\":119304,\"message\":0.0297,\"country\":\"CA\",\"sunrise\":1413804390,\"sunset\":1413842906},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03n\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":273.05,\"humidity\":84,\"pressure\":1012.188,\"temp_min\":273.05,\"temp_max\":273.05},\"wind\":{\"speed\":1.11,\"deg\":260.506},\"rain\":{\"3h\":0},\"clouds\":{\"all\":44},\"dt\":1413780770,\"id\":6094817,\"name\":\"Ottawa\",\"cod\":200}\n"
I'm new to JSON, node.js and javascript... I have a small node.js server that fetch a JSON object(named weatherData below) from openweathermap and I'm trying to make it display only a specific part of the JSON object(a section called "temp"). So far I used:
var tempString = JSON.stringify(weatherData);
And then I used search and substring methods of string to get my info but there's got to be an easier way right? How do I only display the temp section without using string methods?
EDIT: When I stringify the whole thing I get this
Share Improve this question edited Oct 20, 2014 at 5:17 user2997154 asked Oct 20, 2014 at 5:06 user2997154user2997154 4557 silver badges20 bronze badges 5"{\"coord\":{\"lon\":-75.7,\"lat\":45.41},\"sys\":{\"type\":3,\"id\":119304,\"message\":0.0297,\"country\":\"CA\",\"sunrise\":1413804390,\"sunset\":1413842906},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03n\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":273.05,\"humidity\":84,\"pressure\":1012.188,\"temp_min\":273.05,\"temp_max\":273.05},\"wind\":{\"speed\":1.11,\"deg\":260.506},\"rain\":{\"3h\":0},\"clouds\":{\"all\":44},\"dt\":1413780770,\"id\":6094817,\"name\":\"Ottawa\",\"cod\":200}\n"
-
1
Why don't you do
JSON.stringify(weatherData.temp);
? – thefourtheye Commented Oct 20, 2014 at 5:07 - 1 This is pletely backwards. You have the data in javascript object form where you can directly access all the parts. You do NOT want to stringify it and turn it into an easter egg hunt to find what you want. Show us what the JSON data looks like and point out exactly what you want out of the data. – jfriend00 Commented Oct 20, 2014 at 5:12
- I tried that but it prints "undefined". I thought this might be because it's not actually called temp but if I print the whole JSON it displays something like this: {\"temp\":273.05,\"humidity\":84,\"pressure\":1012.188,\"temp_min\":273.05,\"temp_max\":273.05},\"wind\":{\"speed\":1.11,\"deg\":260.506},\"rain\":{\"3h\":0},\"clouds\":{\"all\":44},\"dt\":1413780770,\"id\":6094817,\"name\":\"Ottawa\",\"cod\":200}\n" – user2997154 Commented Oct 20, 2014 at 5:13
- Please put the JSON in your question and format it properly (use the Edit feature). Multi-line pieces of code are not very readable in ments. – jfriend00 Commented Oct 20, 2014 at 5:14
- It looks like maybe you're double stringifying. – jfriend00 Commented Oct 20, 2014 at 5:50
3 Answers
Reset to default 2The JSON is already a native Javascipt object, but weatherData.temp
is undefined
because that's not one of the object's properties. What you want is nested under main
:
weatherData.main.temp // 273.05
Since you mention you're new to JS / Node, it may be helpful to know about console.log() and util.inspect(). You can output an object to the console to inspect its structure using these tools.
Its already is in JSON format, you don't need to convert it into a string.
You can access the "temp" section directly by using weatherData.temp
have you tried?
var result = JSON.stringify(weatherData.temp);