Last Updated: February 25, 2016
·
650
· jonabrams

Easy JSON Formatting for Browsers

If you're writing a web page that needs to display JSON, you probably want to display it nicely with proper indentation.

There's no need to look for a javascript library, the browser can do it for you!

JSON.stringify({hi:"there",arr:[1,2,2],reality:true,child:{}}, null, 2)

Will give you a string with this:

{
  "hi": "there",
  "arr": [
    1,
    2,
    2
  ],
  "reality": true,
  "child": {}
}

The trick is the 3rd parameter to JSON.stringify(). It tells the browser's JSON encoder to indent by that many spaces.

Learn more