Print JSON parsed object?
Solution 1
Most debugger consoles support displaying objects directly. Just use
console.log(obj);
Depending on your debugger this most likely will display the object in the console as a collapsed tree. You can open the tree and inspect the object.
Solution 2
You know what JSON stands for? JavaScript Object Notation. It makes a pretty good format for objects.
JSON.stringify(obj)
will give you back a string representation of the object.
Solution 3
If you want a pretty, multiline JSON with indentation then you can use JSON.stringify
with its 3rd argument:
JSON.stringify(value[, replacer[, space]])
For example:
var obj = {a:1,b:2,c:{d:3, e:4}};
JSON.stringify(obj, null, " ");
or
JSON.stringify(obj, null, 4);
will give you following result:
"{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}"
In a browser console.log(obj)
does even better job, but in a shell console (node.js) it doesn't.
Solution 4
try console.dir()
instead of console.log()
console.dir(obj);
MDN says console.dir()
is supported by:
- FF8+
- IE9+
- Opera
- Chrome
- Safari
Solution 5
to Print JSON parsed object just type
console.log( JSON.stringify(data, null, " ") );
and you will get output very clear
Related videos on Youtube
Comments
-
Skizit about 4 years
I've got a javascript object which has been JSON parsed using
JSON.parse
I now want to print the object so I can debug it (something is going wrong with the function). When I do the following...for (property in obj) { output += property + ': ' + obj[property]+'; '; } console.log(output);
I get multiple [object Object]'s listed. I'm wondering how would I print this in order to view the contents?
-
BiAiB over 13 yearsas a sidenote, for (property in obj) will list all properties, even the inherited ones. So you will get a lot of extraneous one cominng for Object.prototype and any 'mother class'. This is unconvenient with json objects. You have to filter them with hasOwnProperty() to get only the properties that this object owns.
-
-
jasonscript over 10 yearsOnly available in IE9+
-
oHo over 10 years
console.dir()
is also available in FF8+, Opera, Chrome and Safari: developer.mozilla.org/en-US/docs/Web/API/console.dir -
Shahar over 9 yearsIt's worth mentioning that in chrome (and perhaps other browsers) when combined with a string like this:
console.log("object: " + obj)
it does not display the object, but instead will output "object: [Object obj]". -
Hoang Le almost 9 yearsGreat! This is the best solution for me. Thanks.
-
Mingyu almost 9 yearsI'm surprised this answer is at the bottom...... This should be the accepted answer :-)
-
David over 8 yearsnice reference answer
-
Dave Anderson over 8 years@Shahar
console.log("object: %O", obj)
(Chrome) orconsole.log("object: %o", obj)
(Firefox|Safari) will give you access to the object details, see my answer below. -
SuperUberDuper over 8 yearsWhat if you don't want a string representation, but rather the object as it would appear in a code editor?
-
cHao over 8 years@SuperUberDuper: ...Then you wouldn't be trying to build a string representation, now, would you. :)
-
Xsmael over 8 yearswhat about node js ?
-
everton about 8 years%O is really helpful
-
lekant over 7 years@DaveAnderson good shot for the object formatting in the console.
-
Leo Flaherty almost 7 years@Shahar thanks, yours was the information I needed. Should be added to the answer.
-
jasonleonhard over 6 yearsI believe SuperUberDuper was asking if the object could be logged or viewed without converting it to a string. If viewing in the browser the DOM needs an element, you can stringify json so and set an elements contents innerHTML to that string to view it on the page.
-
jasonleonhard over 6 yearsFor example: import Json from './data.json'; var el = document.createElement('div'); el.innerHTML = JSON.stringify(Json);
-
Shahar over 6 yearsIn addition to @DaveAnderson 's method, using a comma to separate strings from objects can also work:
console.log("My object: ", obj)
-
SourceOverflow about 6 yearsThe better solution to the here mentioned methods is simply
console.log({obj})
. (better as in better readable output and less to write, but still clear syntax) -
Adil Saju over 5 years@Shahar Exactly. thats why I've reached in this page.
-
Nassim over 5 yearsgood , but does not support string concatenation like log("string " + variable)
-
Anthonius about 4 yearscould you please describe what does %O for? should it be O specifically? - your solution work like a charm
-
mbenhalima about 4 yearsO stands for object, so as long as the object can be printing as a string, it should be printed with no issues. This has helped me troubleshoot in many cases where I wasn't sure where the error is
-
Anthonius about 4 yearsI forgot to inform here, actually we don't need to use %O. We can directly use console.log("object: ", obj) thank you @mbenhalima
-
MarsAndBack over 3 yearsSo then this is the same as the accepted answer.
-
MarsAndBack over 3 yearsFYI, in Firefox, %O now seems to output as an expandable object inside the console.