Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use:

var str = JSON.stringify(obj, null, 2); // spacing level = 2

If you need syntax highlighting, you might use some regex magic like so:

function syntaxHighlight(json) {
    if (typeof json != 'string') {
         json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

See in action here: jsfiddle

Or a full snippet provided below:

Show code snippet

function output(inp) {
    document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}

function syntaxHighlight(json) {
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};
var str = JSON.stringify(obj, undefined, 4);

output(str);
output(syntaxHighlight(str));
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from user123444555621 on Stack Overflow
🌐
JSONLint
jsonlint.com β€Ί json-pretty-print
JSON Pretty Print - Format & Beautify JSON Online | JSONLint | JSONLint
Pretty print JSON instantly with our free online formatter. Customize indentation, sort keys, and beautify minified JSON. No signup required.
🌐
JSON Formatter
jsonformatter.org β€Ί json-pretty-print
Best JSON Pretty Print Online
Best JSON Pretty Print tool to Make Pretty JSON and JSON Print in color and Save and Share Online.
People also ask

How do I pretty print JSON?
Paste your JSON in the input above and click Format. This tool pretty prints JSON automatically with your chosen indent size (2 spaces, 4 spaces, or tabs).
🌐
json-indent.com
json-indent.com
JSON Formatter & Beautifier β€” Free, 100% In-Browser
How do I beautify JSON online?
Paste your minified or unformatted JSON into the input panel above. This free online JSON beautifier instantly adds indentation and line breaks to make it readable, with nothing uploaded to a server.
🌐
json-indent.com
json-indent.com
JSON Formatter & Beautifier β€” Free, 100% In-Browser
How do I format JSON online for free?
Paste your JSON into the input panel above. Formatting happens automatically. This tool is completely free with no account, no signup, and no usage limits.
🌐
json-indent.com
json-indent.com
JSON Formatter & Beautifier β€” Free, 100% In-Browser
🌐
JSON Formatter
jsonformatter.curiousconcept.com
JSON Formatter & Validator
In order to keep focused on providing the best JSON beautifier and validator online, we do not offer an offline version.
🌐
Zerodevx
zerodevx.github.io β€Ί json-pretty-print
JSON Pretty Print Online
Convert unformatted JSON into pretty-printed JSON and send the view as a shareable web link.
🌐
CodeBeautify
codebeautify.org β€Ί jsonviewer
Best JSON Viewer and JSON Beautifier Online
Online JSON Viewer, JSON Beautifier and Formatter to beautify and tree view of JSON data - It works as JSON Pretty Print to pretty print JSON data.
Find elsewhere
Top answer
1 of 16
6934

Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use:

var str = JSON.stringify(obj, null, 2); // spacing level = 2

If you need syntax highlighting, you might use some regex magic like so:

function syntaxHighlight(json) {
    if (typeof json != 'string') {
         json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

See in action here: jsfiddle

Or a full snippet provided below:

Show code snippet

function output(inp) {
    document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}

function syntaxHighlight(json) {
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};
var str = JSON.stringify(obj, undefined, 4);

output(str);
output(syntaxHighlight(str));
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 16
441

User Pumbaa80's answer is great if you have an object you want pretty printed. If you're starting from a valid JSON string that you want to pretty printed, you need to convert it to an object first:

var jsonString = '{"some":"json"}';
var jsonPretty = JSON.stringify(JSON.parse(jsonString),null,2);  

This builds a JSON object from the string, and then converts it back to a string using JSON stringify's pretty print.

🌐
Js
pretty-print-json.js.org
Pretty Print JSON
Pretty-Print JSON -- Interactive online JavaScript tool to format JSON
🌐
JSON Indent
json-indent.com
JSON Formatter & Beautifier β€” Free, 100% In-Browser
Paste your JSON in the input above and click Format β€” this tool pretty prints JSON automatically with your chosen indent size (2 spaces, 4 spaces, or tabs). ... Paste your minified or unformatted JSON into the input panel above. This free online JSON beautifier instantly adds indentation and line breaks to make it readable β€” no signup, and nothing is uploaded to a server.
🌐
Chrome Web Store
chromewebstore.google.com β€Ί detail β€Ί json-pretty β€Ί nflbalmkceonkkhifbifebanhladdgcp
JSON Pretty - Chrome Web Store
Themes help you customize the experience to make json pretty and readable in any condition. 5️⃣ Secure. The extension works with your data locally. You can paste or upload source directly from your computer. 6️⃣ Syntax Highlighting. Syntax highlighting simplifies print and transforms how you see JSON pretty format online.
🌐
JSON Pretty
json-pretty.com
JSON Pretty β€” Powerful Online Formatter, Editor & Validator
JSON Pretty is a modern JSON formatter, editor and validator that helps you work with JSON with 100% privacy and native-app speed in your browser.
🌐
JSON Formatter
jsonformatter.org β€Ί javascript-pretty-print
Best JavaScript Pretty Print to Pretty JavaScript and Print JavaScript
Free and Easy to use JavaScript Pretty Print tool to Make JavaScript Pretty and Javascript Print in color.
🌐
Online Tools
onlinetools.com β€Ί json β€Ί prettify-json
Prettify JSON – Online JSON Tools
Free online JSON beautifier. Just load your JSON in the input field and it will automatically get prettified. In the tool options, you can choose whether to use spaces or tabs for indentation and if you're using spaces, you can specify the number ...
🌐
JSON2XML
json2xml.com β€Ί json-pretty-print
Free JSON Pretty Print Online β€” json2xml.com
Pretty-print JSON with configurable indentation (2 spaces, 4 spaces, tabs). Free, private β€” your data never leaves your machine. No sign-up required.
🌐
Casperlee
casperlee.com β€Ί en β€Ί tools β€Ί jsonformatter
Online JSON Formatter
English | δΈ­ζ–‡ Β· Online JSON Formatter Β· Put the JSON data here: Β· Process Β· The formatted code will be shown here
🌐
MConverter
mconverter.eu β€Ί convert β€Ί txt β€Ί json
TXT to JSON Converter β€’ Online & Free β€’ MConverter
Batch convert TXT to JSON online. Convert large TXT files up to 10 GB each. Fast and easy exporting from TXT to JSON in bulk.
🌐
Dillinger
dillinger.io
Markdown Editor β€” Online, Free, with Live Preview | Dillinger
Dillinger is widely considered one of the best free online markdown editors because it combines a Monaco-powered editor, live preview, and cloud sync to GitHub, Dropbox, Google Drive, OneDrive, and Bitbucket β€” with no signup, no paywall, and ...
🌐
Neverendingqs
pprint-ndjson.neverendingqs.com
Pretty-Print Newline Delimited JSON
Newline Delimited JSON pretty printer (e.g. ndjson, JSONlines).
🌐
W3Schools
w3schools.com β€Ί js β€Ί js_json.asp
W3Schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
JsonFormatter
jsonformatter.org β€Ί json-editor
Best JSON Editor Online
Python Pretty Print JSON Β· Read JSON File Using Python Β· Validate JSON using PHP Β· Python Load Json From File Β· Best and Secure Online JSON Editor works well in Windows, Mac, Linux, Chrome, Firefox, Safari and Edge. This JSON Editor supports edit JSON File.