0

I am trying to get JSON objects from a JSON file, it has nested JSON objects (which has arrays as well). SO how do I parse it and get the individual elements and objects using NODEJS?

I have tried this

const express = require('express');
const app = express();
const request = require("request");
reg = "Dhaka" 
link =  'https://api.worldweatheronline.com/premium/v1/weather.ashx?key=ca926a35ffc14b97b0993747192010&q='+reg+'&format=json&num_of_days=5&extra=localObsTime&date=today&fx=yes&cc=yes&fx24=yes&includelocation=yes&tp=3'
let bodydata = "";
request (link, function (error, response, body) {
    console.log('error:', error); 
    console.log('statusCode:', response && response.statusCode); 
    // console.log('body:', body); // Print the HTML for the Google homepage.
    bodydata = body 
    // console.log (bodydata)
    let jpar = JSON.parse (bodydata)
    datab = bodydata.data
    console.log(jpar)
});

This is the output.

error: null
statusCode: 200
{ data:
   { request: [ [Object] ],
     nearest_area: [ [Object] ],
     current_condition: [ [Object] ],
     weather: [ [Object] ],
     ClimateAverages: [ [Object] ] } }

It is getting the response, but it is coming out as "undefined", when I am trying to print the datab variable.

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
S_Chakra
  • 27
  • 1
  • 9
  • *"but it is coming out as "undefined" , when I am trying to print the bodydata variable."* What's printing out the `{data: {...}}` part then? – Felix Kling Nov 05 '19 at 08:07
  • this ```error: null statusCode: 200 undefined ``` – S_Chakra Nov 05 '19 at 08:09
  • Sorry if my comment wasn't clear: In your question you are saying that the outpout is `{data: {...}}`. Where is that information coming from? Is `console.log` outputting this? If yes, which one? – Felix Kling Nov 05 '19 at 08:10
  • oh not that, the output is just to check if I have parsed it correctly. But, when I try to get the individual objects, it is coming out as undefined. The output here is coming from ``` let jpar = JSON.parse (bodydata) console.log(jpar) ``` – S_Chakra Nov 05 '19 at 08:13
  • And when exactly are you getting `undefined`? What's the exact command? – Felix Kling Nov 05 '19 at 08:38
  • when I am trying to print the *datab* variable – S_Chakra Nov 05 '19 at 08:40
  • console.log (datab); – S_Chakra Nov 05 '19 at 08:40
  • `bodydata.data` is `undefined` because `bodydata` is a string and strings don't have a `data` property. `jpar` contains the parsed data (an object) so it should be `jpar.data`. – Felix Kling Nov 05 '19 at 09:16

2 Answers2

1

Not sure what data you're actually trying to get but if you want to make an api call and render the results in express then your code should look like this:

var express = require('express');
var request = require('request');
var app = express();

app.get('/', function (req, res) {
    var reg = 'Dhaka';
    request('https://api.worldweatheronline.com/premium/v1/weather.ashx?key=ca926a35ffc14b97b0993747192010&q=' + reg + '&format=json&num_of_days=5&extra=localObsTime&date=today&fx=yes&cc=yes&fx24=yes&includelocation=yes&tp=3', function (error, response, body) {
        // parse JSON so we have an object to work with
        var weather = JSON.parse(body).data.weather;
        // send data to browser
        res.send(weather);
    });
});

// Run express server on port 3000
app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});
Furkan Poyraz
  • 672
  • 1
  • 4
  • 14
  • This did it!! since I was stupid enough to not put it in the app. MANY THANKS!! – S_Chakra Nov 05 '19 at 09:02
  • Hi, sorry to bother you again, but can you tell me how I can get the date, since date is another JSON object inside the data object. I wrote JSON.parse (weather.date), but that doesn't seem to work. Even wrote, weather.data, and that did not work. – S_Chakra Nov 05 '19 at 09:11
  • @S_Chakra The weather variable returns an array, so you'd need to loop over it and display each date but since the array has only one item you can do: res.send(weather[0].date); – Furkan Poyraz Nov 05 '19 at 09:16
  • 1
    @S_Chakra: This may help: [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/q/11922383/218196) – Felix Kling Nov 05 '19 at 09:17
  • Thanks again, I would have given you chocolate if I could. – S_Chakra Nov 05 '19 at 09:30
0

you should get data from jpar variable, not from bodydata

let jpar = JSON.parse (bodydata)
datab = jpar.data
console.log(datab)
Channa
  • 3,267
  • 7
  • 41
  • 67