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?

Joel Hoelting
  • 1,862
  • 2
  • 23
  • 45
  • 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. – Pointy Apr 20 '19 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. – Pointy Apr 20 '19 at 20:16
  • You might want to console.log(util.inspect(body)) – teroi Apr 20 '19 at 20:17
  • 2
    here https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object – 1565986223 Apr 20 '19 at 20:18
  • @Pointy `console.dir` still logs out the JSON in the same way (with [Object]) – Joel Hoelting Apr 20 '19 at 20:23
  • @JoelHoelting OK well that `util.inspect()` thing that a couple other people pointed out seems like what you want. – Pointy Apr 20 '19 at 20:24
  • @teroi: `console.log(util.inspect(data, {showHidden: false, depth: null}));` did what I wanted. I really don't understand the reason behind the obfuscation in console.log. It doesn't make my life easier (but that's a subjective opinion I suppose). – Joel Hoelting Apr 20 '19 at 20:25
  • 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. – Pointy Apr 20 '19 at 20:28
  • @Pointy: thanks for the clarification. I guess I would have to ask the node developers why they decided to implement `console()` to hide nested objects. – Joel Hoelting Apr 20 '19 at 20:34

2 Answers2

5

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))

Catalyst
  • 3,143
  • 16
  • 20
3

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)));
}
baao
  • 71,625
  • 17
  • 143
  • 203