4

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?

4
  • what type of response are you getting in body?? Commented Jul 23, 2018 at 6:05
  • the actual json Commented Jul 23, 2018 at 6:10
  • @Hardik BODY IS ={ "columns" : [ "Items" ], "rows" : [ { "Item" : "ABC" }, { "Item" : "DEF" } ] Commented Jul 23, 2018 at 6:12
  • response ({ "columns" : [ "Items" ], "rows" : [ { "Item" : "ABC" }, { "Item" : "DEF" } ]) looks like a object you don't have to parse it again. Commented Jul 23, 2018 at 6:24

3 Answers 3

3

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

1 Comment

or you can add comma as a seperator in console.log. console.log("JSON IS =", jsonResponse.rows);
0

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.

2 Comments

that will not display the actual object rather a simple string which is very difficult to read/debug.
he's using + with string and object that is the problem. he needs two console.log statements or separate current one with , instead of +
0
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)
    }
});

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.