0

When I use JSON.parse and log out some fetched data with the require module, nested objects are logged [Object]. Here is an example (currently using Node version 10.15):

const request = require("request");

const url = "https://www.reddit.com/r/javascript.json";

request(url, (error, response) => {
  const data = JSON.parse(response.body);
  console.log(data)
});

{ kind: 'Listing',
  data:
   { modhash: '',
     dist: 26,
     children:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     after: 't3_bf4cl6',
     before: null } }

I was looking at this question: JSON.parse returns [object] from JSON

The person asking is curious why after JSON.parse, objects get logged as [Object]. The top answer states that the reason JSON.parse hides the data is for readability. But the answer doesn't explain a way to override the default behavior.

How do I get JSON.parse to log the full data? Is there a way to override the default behavior?

9
  • 9
    This has nothing to do with JSON.parse(). That returns a JavaScript object, and you're logging it out through the console mechanism. That is what's responsible for showing you the object value that way. Commented Apr 20, 2019 at 20:15
  • 1
    You can try console.dir() instead of console.log(), or write your own function to traverse the object graph and dump out the value exactly how you want to see it. Commented Apr 20, 2019 at 20:16
  • You might want to console.log(util.inspect(body)) Commented Apr 20, 2019 at 20:17
  • 2
    here stackoverflow.com/questions/10729276/… Commented Apr 20, 2019 at 20:18
  • 2
    The console() mechanism has no standard; it's something that various JavaScript platforms provide (browsers, Node) but it's pretty much up to the individual implementors to decide what it does. Commented Apr 20, 2019 at 20:28

2 Answers 2

4

If logging response.body directly is not formatted the way you like, and that is why you are doing JSON.parse, then to log the full formatted object do console.log(JSON.stringify(JSON.parse(response.body), null, 2))

Sign up to request clarification or add additional context in comments.

Comments

2

That's not JSON.parse, but console.log's default behavior for nested objects. You can use node's util.inspect to log with depth

const {inspect} = require('util');
console.log(inspect(obj, false, null, true));

Or as a function

function log(...data) {
  data.forEach(d => console.log(inspect(d, false, null, true)));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.