How can I beautify JSON programmatically? [duplicate]
This question already has an answer here:
“name” : “Steve”,
“surname” : “Jobs”,
“company” : “Apple”
some_magic(jsonObj); // return beautified JSON
Programmatic formatting solution:
JSON.stringify method supported by many modern browsers (including IE8) can output a beautified JSON string:
JSON.stringify(jsObj, null, "\t"); // stringify with tabs inserted at each level JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level
This method is also included with json2.js, for supporting older browsers.
Manual formatting solution
If you don’t need to do it programmatically, Try JSON Lint. Not only will it prettify your JSON, it will validate it at the same time.