1

I am totally new to Node.JS. I have written the Node.JS code and getting the response in JSON file. The JSON is in Nested Format. I want to get, store in a variable and print only one value inside the Nested JSON.

Code:

var https = require('https');
var optionget = {
 host : 'api-dev.dataoverflow.com',
 port : 443,
 path : '/test1/test2/MR0000091/benifits?latency=RTIME',
 method : 'GET',
 HEADERS: {
  'Authorization' : 'Basic Qjrfrfhurhfurhjfr2839gbfwj==',
  'X-PruAppID : 'PlainCardPortal'
  }
 };

console.info(optionsget)

var reqGet = htttps.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});

reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});

var optionsgetmsg = {
 host : 'api-dev.dataoverflow.com',
 port : 443,
 method : 'GET'
};

console.info(optionsgetmsg);

var reqGet = https.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
res.setEncoding('utf-8')

    res.on('data', function(data) {
        process.stdout.write(data);
    });
});

reqGet.end();
reqGet.on('error', function(e) {
 console.error(e);
});

Getting the whole JSON:

{
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "image":
        {
            "url": "images/0001.jpg",
            "width": 200,
            "height": 200
        },
    "thumbnail":
        {
            "url": "images/thumbnails/0001.jpg",
            "width": 32,
            "height": 32
        }
}

I want to get the width=32 only which is under thumbnail. I want to store it in a variable and want to print in console.

1
  • htttps.request Is this normal ? Commented Mar 14, 2019 at 13:37

4 Answers 4

2

You can use JSON.parse() in order to convert your JSON string into an object, and then you can retrieve your value as you would a regular JS object like this:

let parsedData = JSON.parse(data)
process.stdout.write(console.log(parsedData.image.url)
Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like

res.on('data', (d) => {
  var jsonObj = JSON.parse(d);
  console.log('width = ', jsonObj.thumbnail.width);
});

JSON.parse() will transform the received string in d to a JavaScript object. You then can access the object via standard dot notation.

Comments

1

Parse the string using JSON.parse(...). The object is then a standard JavaScript object. You can access it using either dot notation (e.g. object.property) or via index notation (e.g. object['property'])

You can also nest those, such as in your example, data.thumbnail.width

res.on('data', (d) => {
    let data = JSON.parse(d);
    let width = data.thumbnail.width;
    console.log(width);
});

However, if thumbnail could possibly be undefined, you should check for that.

res.on('data', (d) => {
    let data = JSON.parse(d);
    let width = undefined;
    if (data.thumbnail) width = data.thumbnail.width;
    console.log(width);
});

Comments

0

Use npm request

var request = require('request');

var options = {
    url: 'api-dev.dataoverflow.com:443/test1/test2/MR0000091/benifits?latency=RTIME',
    headers: {
        'Authorization': 'Basic Qjrfrfhurhfurhjfr2839gbfwj==',
        'X-PruAppID': 'PlainCardPortal'
    }
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

Comments

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.