Do you know of any "JSON Beautifier" for JavaScript?
From
{"name":"Steve","surname":"Jobs","company":"Apple"}
To
{
 "name" : "Steve",
 "surname" : "Jobs",
 "company" : "Apple"
}
Example
some_magic(jsonObj); // return beautified JSON
 
 asked Apr 10, 2010 at 20:30
 
 
 
 Randy Mayer 
 
 8,9358 gold badges27 silver badges11 bronze badges
 
 - 
 Why do you need to beautify it programmatically? Is it being displayed on a web page?SeanJA– SeanJA2010年04月10日 20:49:52 +00:00Commented Apr 10, 2010 at 20:49
 - 
 3I'm rather amused to see so many "solutions" referenced in the answers, all solving a problem that is, per Andy E's answer, already catered for by the standard API. A lesson to us all: read the documentation of existing APIs before either seeking or implementing a solution to a requirement ;-)NickFitz– NickFitz2011年07月10日 01:30:23 +00:00Commented Jul 10, 2011 at 1:30
 - 
 If someone is looking for this stackoverflow.com/a/39183263/9295698Kesava Karri– Kesava Karri2024年05月21日 23:39:56 +00:00Commented May 21, 2024 at 23:39
 
1 Answer 1
Programmatic formatting solution:
The 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
Demo: http://jsfiddle.net/AndyE/HZPVL/
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.
 answered Apr 10, 2010 at 20:33
 
 
 
 Andy E 
 
 346k86 gold badges482 silver badges452 bronze badges
 
 
 Sign up to request clarification or add additional context in comments.
 
 
 
 8 Comments
timborden
 Awesome...thanks! (It might be worth adding that 
  white-space:pre in the css is needed as well)codefreak
 on html page, wrap your output in <pre></pre> tags
  absynce
 I use JSON.stringify and toastr to output for debugging. Live Example 
  tandrewnichols
 Knew about 
  null, 2, but didn't know that '\t' would work too. Fantastic!luckybroman5
 I find jsonlint.com a little cumbersome. A MUCH quicker workflow is kadebom.com/jsonlint.html IMO
   | 
 Explore related questions
See similar questions with these tags.
lang-js