Last Updated: February 25, 2016
·
49.01K
· felipeelias

Pretty print JSON with native Javascript

The native JSON.stringify has an option to make the output more readable by adding spaces to the string.

Example:

var someObject = { "test": 123, "others": [ { "abc": 1 }, { "xyz": 999 } ] };

JSON.stringify(someObject, undefined, 2);

The return will be:

{
  "test": 123,
  "others": [
    {
      "abc": 1
    },
    {
      "xyz": 999
    }
  ]
}

The second parameter is a replacer, which basically works like a filter to ignore certain values.

For more info: https://developer.mozilla.org/en-US/docs/Using_native_JSON

1 Response
Add your response

This is awesome!!.. and extremely simple. There are other alternatives but using other libs and stuff.. this is the best :D Thanks

over 1 year ago ·