Friday 9 June 2017

Data query from API JSON object (NodeJs)

I am working on simple Nodejs app that will use Yahoo weather API to get Sunset time for Hawaii

App will send request to Endpoint of Sunset time url of yahoo weather API and response with body string object:

var request = require('request');
    console.log("Sunset in Hawaii is at...")
request('https://query.yahooapis.com/v1/public/yql?q=select%20astronomy.sunset%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22maui%2C%20hi%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys', function (error, response, body) {
         if(!error && response.statusCode == 200){
             var parsedData = JSON.parse(body) // convert body string to Json object
             console.log(parsedData['query']['results']['channel']['astronomy']['sunset']);
         } 
});

I converted body to JSON object and printed Sunset time using nested query:

parsedData['query']['results']['channel']['astronomy']['sunset']

JSON object:

{ query: 
   { count: 1,
     created: '2017-06-09T08:36:39Z',
     lang: 'en-US',
     results: { channel: { astronomy: { sunset: 7:5 pm } } } } };

is there any simple way to get sunset time from JSON object other than nested query ?



via Colt

No comments:

Post a Comment