Sydnie
Sydnie

Reputation: 71

JSON.parse returns object object

I am trying to get the values of item, but when I print the values in the console it displays object instead of the values. How can I access the values?

Here is my code:

var options2;
request.post(opts,function(error,response,body){
    if (!error && response.statusCode == 200 || response.statusCode == 201) {
        var jsonResponse = JSON.parse(body);
        console.log("JSON IS =" + jsonResponse.rows);
        options2 = jsonResponse.rows.reduce((acc, obj) => acc.concat(obj['Item']), []);
    }else{
        console.log(body)
    }
});

What do I miss?

Upvotes: 4

Views: 2663

Answers (3)

ishan shah
ishan shah

Reputation: 603

Print in a new line like:

console.log("JSON IS =");
console.log(jsonResponse.rows);

Or replace '+' with a ',' like this,

console.log("JSON IS = ", jsonResponse.rows);

Upvotes: 3

Hardik
Hardik

Reputation: 3258

var options2;
request.post(opts,function(error,response,body){
    if (!error && response.statusCode == 200 || response.statusCode == 201) {
        var jsonResponse = JSON.parse(body);
        // **wrap rows in JSON.stringify()**
        console.log("JSON IS =" + JSON.stringify(jsonResponse.rows));
        options2 = jsonResponse.rows.reduce((acc, obj) => acc.concat(obj['Item']), []);
    }else{
        console.log(body)
    }
});

Upvotes: 0

Pavel Vasiluk
Pavel Vasiluk

Reputation: 317

If the main idea is just to debug object data (doing console logging), you may want to use json.stringify method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify That will give you string representation of an object.

Upvotes: 0

Related Questions