Print JSON parsed object?


Print JSON parsed object?

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]+’; ‘;

I get multiple [object Object]’s listed. I’m wondering how would I print this in order to view the contents?


Solution 1:

Most debugger consoles support displaying objects directly. Just use


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:

try console.dir() instead of console.log()


MDN says console.dir() is supported by:

  • FF8+
  • IE9+
  • Opera
  • Chrome
  • Safari

Solution 4:

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, "    ");


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 5:

to Print JSON parsed object just type

console.log( JSON.stringify(data, null, " ") );

and you will get output very clear

Solution 6:

Use string formats;

console.log("%s %O", "My Object", obj);

Chrome has Format Specifiers with the following;

  • %s Formats the value as a string.
  • %d or %i Formats the value as
    an integer.
  • %f Formats the value as a floating point value.
  • %o Formats the value as an expandable DOM element (as in the
    Elements panel).
  • %O Formats the value as an expandable JavaScript
  • %c Formats the output string according to CSS styles you

Firefox also has String Substitions which have similar options.

  • %o Outputs a hyperlink to a JavaScript object. Clicking the link opens an inspector.
  • %d or %i Outputs an integer. Formatting is not yet supported.
  • %s Outputs a string.
  • %f Outputs a floating-point value. Formatting is not yet supported.

Safari has printf style formatters

  • %d or %i Integer
  • %[0.N]f Floating-point value with N digits of precision
  • %o Object
  • %s String