buwfjw
Last Updated: February 25, 2016
·
13.64K
· felipeelias
F6faa607e4f541da71fbf8aea6f99a7a

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

Say Thanks
Respond

1 Response
Add your response

21193
None

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 ·