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.