Last Updated: February 25, 2016
·
960
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside