Reputation: 25
To be as concise as possible, I'm trying to figure out if there is a way to allow the console to display the full contents of an object using console.log(). I am using node.js piped into the console within Sublime Text 3. I'm having no problems running the code itself, just displaying the output.
I have a linked list object that I created like so:
{ value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5, next: null}}}}}
When it outputs to the console it appears similar to the following:
{ value: 1, next:
{ value: 2, next: { value: 3, next: [Object]}}
In short, the entirety of the object is not visible or expandable in any way. Is there any way I can force the console to be more verbose and display the entire object?
Upvotes: 2
Views: 1845
Reputation: 3591
One way you can do is by converting your JSON to a string.
So, if your file content is fetched in a variable x. You can do:
console.log(JSON.stringify(x));
This will convert your JSON data to a string which can be easily printed to the console.
Here is another workaround:
console.log(require('util').inspect(x, {showHidden: false, depth: null}));
The thing is that console.log is made to print strings, not objects. So, it automatically tries to convert everything to a string and in the process (since automatic casting is not the right way to convert objects to strings) some data is lost.
You can use the second workaround, but I'd strongly suggest that you stick to the first one since it's the better approach.
Upvotes: 2