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.

8
  • "but it is coming out as "undefined" , when I am trying to print the bodydata variable." What's printing out the {data: {...}} part then? Commented Nov 5, 2019 at 8:07
  • this error: null statusCode: 200 undefined Commented Nov 5, 2019 at 8: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? Commented Nov 5, 2019 at 8: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) ``` Commented Nov 5, 2019 at 8:13
  • And when exactly are you getting undefined? What's the exact command? Commented Nov 5, 2019 at 8:38

2 Answers 2

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

5 Comments

This did it!! since I was stupid enough to not put it in the app. MANY THANKS!!
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 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);
Thanks again, I would have given you chocolate if I could.
0

you should get data from jpar variable, not from bodydata

let jpar = JSON.parse (bodydata)
datab = jpar.data
console.log(datab)

1 Comment

yeah, but I am not. The datab was just to check if I have parsed it or not.

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.