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?
JSON.parse(). That returns a JavaScript object, and you're logging it out through theconsolemechanism. That is what's responsible for showing you the object value that way.console.dir()instead ofconsole.log(), or write your own function to traverse the object graph and dump out the value exactly how you want to see it.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.