Javascript: How to generate formatted easy-to-read JSON straight from an object? [duplicate]

0 votes
asked Aug 18, 2010 by ben-clayton

Possible Duplicate:
How can I beautify JSON programmatically?

I know how to generate JSON from an object using JSON.stringify, or in my case the handy jquery-json from google code (

Now this works fine, but the output is hard to read for humans. Is there an easy way / function / whatever to output a neatly formatted json file?

This is what I mean:




I'd like something like this instead:


E.g. with newlines and tabs added. It's much easier to read for larger documents.

I'd like to do this ideally without adding any huge libraries - e.g. not prototype or YUI or whatever.

1 Answer

0 votes
answered Aug 18, 2010 by cristian-sanchez

JSON.stringify takes more optional arguments.


 JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces
 JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, "\t"); // Indented with tab


How can I beautify JSON programmatically?

Should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don't support the JSON helper functions.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter