How to beautify a JSON

Some time ago I need beautify a JSON in a textarea and I found that JSON.stringify() provides a parameter to specify the String to use as a space, so you can set a tab or number of spaces at each JSON level.

Beautify a JSON

Let’s use JSON.stringify() with 2 more parameters.

// This insert tabs at each level 
JSON.stringify(jsonObject, null, "\t"); 

// This put two spaces at each level 
JSON.stringify(jsonObject, null, 2);

Note the third parameter is the character to be used for each line (\t a tab) or number of spaces to tab (in the example we are using 2 spaces)

Read more JSON.stringify()

David Burgos

Read more posts by this author.