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
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.

๐ŸŒ
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.
Discussions

[HOW TO] Trying to display JSON in a readable format.....
There's a PHP function called json_encode() and you can pass the JSON_PRETTY_PRINT flag to make it pretty: echo json_encode( $json, JSON_PRETTY_PRINT ); More on reddit.com
๐ŸŒ r/Wordpress
9
2
November 20, 2023
FracturedJson - a JSON formatter that produces human-readable but fairly compact output
3 years have passed. We now have linting and minifiers for the numerous JSON formatting libs that have been released. There are now 8 competing standards on how best to format JSON so it's pretty. Joking aside, great idea. I've rarely had problems reading formatted JSON, but lining it up as though it's columns is definitely a lot easier on the eyes and for comparing data in arrays. More on reddit.com
๐ŸŒ r/javascript
23
183
July 12, 2021
๐ŸŒ
Js
pretty-print-json.js.org
Pretty-Print JSON โ€ข Interactive online JavaScript tool to format JSON
Pretty-Print JSON -- Interactive online JavaScript tool to format JSON
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ ounkkzpp โ€บ javascript-pretty-print-json-example
How do I pretty print JSON in JavaScript?
In this JavaScript Pretty Print JSON example, we use the JSON.stringify() method to print a JavaScript object in a human-readable format. Click Execute to run the JavaScript Pretty Print JSON example online ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ object โ€บ pretty-print json
Pretty-print a JSON object with JavaScript - 30 seconds of code
July 30, 2022 - This is pretty easy to accomplish, using JSON.stringify() with the appropriate arguments. const obj = { id: 1182, username: 'johnsmith', active: true, emails: ['[email protected]', '[email protected]'], }; JSON.stringify(obj, null, 2); // { ...
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ speaklouder โ€บ how-to-pretty-json-output-using-javascript-27k6
How to Pretty JSON Output using JavaScript - DEV Community
October 3, 2023 - In this example, we have an object jsonData, and we use JSON.stringify() with two additional arguments: null and 2. The null argument is for replacing values or functions, and the 2 specifies the number of spaces to use for indentation. This results in a nicely formatted JSON output with each key-value pair on a new line and indented by 2 spaces. The example above provides a straightforward way to pretty print JSON, but you can further customize it to suit your preferences.
๐ŸŒ
ZetCode
zetcode.com โ€บ javascript โ€บ json-pretty-print
JavaScript JSON Pretty Print - Formatting JSON Data
The JSON.stringify function converts a JavaScript object or value to a JSON string. We can use the function to pretty print JSON output. Note that on terminal, the console.log and console.dir functions automatically pretty print JSON data.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-pretty-print-json-string-in-javascript
How to Pretty Print JSON String in JavaScript? - GeeksforGeeks
July 12, 2025 - const fs = require('fs'); const ... console.log('JSON saved in a pretty format.'); You can display formatted JSON in your web app using <pre> tags....
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-pretty-print-json-using-javascript.php
How to Pretty-print JSON Using JavaScript
// Sample JSON string var json = '{"name": "Peter", "age": 22, "country": "United States"}'; // Converting JSON string to object var obj = JSON.parse(json); // Pretty printing var str = JSON.stringify(obj, null, 4); // spacing level = 4 ...
๐ŸŒ
Leapcell
leapcell.io โ€บ blog โ€บ how-to-pretty-print-json
How to Pretty Print JSON | Leapcell
July 25, 2025 - Online formatters and command-line utilities are quick options for pretty printing. JSON (JavaScript Object Notation) is a widely used data format for storing and exchanging information between servers and web applications. However, JSON data is often transmitted as a single line or in a compact ...
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ pretty-print-json
pretty-print-json - npm
May 25, 2026 - Source is written in functional TypeScript, and pretty-print-json.min.js (minified) is 2.1 KB. Interactive online tool to format JSON: https://pretty-print-json.js.org
      ยป npm install pretty-print-json
    
Published ย  May 25, 2026
Version ย  3.0.8
๐ŸŒ
GitHub
github.com โ€บ center-key โ€บ pretty-print-json
GitHub - center-key/pretty-print-json: ๐Ÿฆ‹ Pretty-print JSON data into HTML to indent and colorize (with TypeScript declarations)
Source is written in functional TypeScript, and pretty-print-json.min.js (minified) is 2.1 KB. Interactive online tool to format JSON: https://pretty-print-json.js.org
Starred by 141 users
Forked by 27 users
Languages ย  TypeScript 60.7% | Shell 39.3%
๐ŸŒ
The Code Barbarian
thecodebarbarian.com โ€บ pretty-json-stringify-output.html
Pretty `JSON.stringify()` Output in JavaScript
The prettyjson npm package is a great way to add neat color formatting to your JSON output. Prettyjson only works on the CLI, you won't get colors if you send prettyjson output as an HTTP response. Below is an example of pretty printing JSON from Node.js using prettyjson:
๐ŸŒ
JSONBin
jsonbin.io โ€บ blog โ€บ 1 โ€บ how-to-pretty-print-a-json-object-with-javascript
How to pretty-print a JSON object with JavaScript - JSONBin.io
Copyconst myObj = { "people": [ { "name": "Mark Vande Hei", "craft": "ISS" }, { "name": "Oleg Novitskiy", "craft": "ISS" }, { "name": "Pyotr Dubrov", "craft": "ISS" } ] }; // serialize myObj const str = JSON.stringify(myObj, null, 2); // pretty print console.log(str);
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ pretty-print-JSON-using-JavaScript
pretty-print JSON using JavaScript
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
JSFiddle
jsfiddle.net โ€บ unLSJ
Pretty Print JSON Data in Color - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
๐ŸŒ
JSON Formatter
jsonformatter.org
Best JSON Formatter and JSON Validator: Online JSON Formatter
It helps to validate JSON online with Error Messages. It's the only JSON tool that shows the image on hover on Image URL in a tree view. It's also a JSON Beautifier that supports indentation levels: 2 spaces, 3 spaces, and 4 spaces. Supports Printing of JSON Data.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ javascript-pretty-print
Javascript Pretty Print online to Prettier Javascript code
Pretty Print Javascript works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. ... Javascript data Try it. var carInsuranceCompany = { name: "Geico", market_capital: "$34.9 billion", }; var carInsuranceCompanyObj = JSON.stringify(obj); document.getElementById("insurance").innerHTML = carInsuranceCompanyObj;