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
JSON Pretty Print is very unique tool for prettify json and pretty print JSON data in color.
🌐
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.
🌐
JSON Formatter
jsonformatter.org
Best JSON Formatter and JSON Validator: Online JSON Formatter
Python Pretty Print JSON Β· Read JSON File Using Python Β· Validate JSON using PHP Β· Python Load Json From File Β· Online JSON Formatter and Online JSON Validator provide JSON converter tools to convert JSON to XML, JSON to CSV, and JSON to YAML also JSON Editor, JSONLint, JSON Checker, and JSON Cleaner.
🌐
JSON Beautifier
jsonbeautifier.org
Json Beautifier - Json Formatter | Json Viewer | Json Editor
Online best free JSON Pritty Print is a tool to easily Json editor, Json viewer and Json formatter online.
🌐
JSON Formatter
jsonformatter.curiousconcept.com
JSON Formatter & Validator
The JSON Formatter & Validator beautifies and debugs JSON data with advanced formatting and validation algorithms.
🌐
Jsontotable
jsontotable.org β€Ί json-pretty-print
JSON Pretty Print - Free Online JSON Beautifier Tool | JSON to Table Converter
Pretty print and beautify JSON data online. Format unreadable JSON with proper indentation, syntax highlighting, and customizable options. Free JSON pretty printer with advanced formatting.
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.

🌐
Zerodevx
zerodevx.github.io β€Ί json-pretty-print
JSON Pretty Print Online - Open Source
Convert unformatted JSON into pretty-printed JSON and send the view as a shareable web link.
🌐
JSON Formatter
jsonformatter.net β€Ί home β€Ί json pretty print
JSON Pretty Print - JSON Formatter
January 30, 2025 - JSON Pretty Print will prettify JSON and allows you to take a print. It works as JSON Prettifier, Beautifier and Viewer.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - The code takes a JSON string containing student records, parses it into a Python data structure, then pretty-prints the JSON data with proper indentation for improved readability.
🌐
Hako IT
hakoit.com β€Ί portrait β€Ί ➭ json formatter & beautifier β€” pretty print json & minify online
➭ JSON Formatter & Beautifier β€” Pretty Print JSON & Minify Online - Hako IT
March 1, 2026 - Free JSON Formatter Online β€” paste or drag and drop any JSON to instantly pretty print it with color syntax highlighting. Use the Minify button to compress JSON to a single line, or switch to Tree View to explore nested objects and arrays ...
🌐
JSON Indenter
jsonindenter.com β€Ί home β€Ί blog β€Ί json pretty print: format and beautify json data
JSON Pretty Print: Format and Beautify JSON Data
March 3, 2026 - Paste minified or messy JSON and get a beautifully formatted result in one click. No sign-up required. JSON Pretty Print Tool
Top answer
1 of 15
3105

Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:

>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
    "foo",
    {
        "bar": [
            "baz",
            null,
            1.0,
            2
        ]
    }
]

To parse a file, use json.load():

with open('filename.txt', 'r') as handle:
    parsed = json.load(handle)
2 of 15
501

You can do this on the command line:

python3 -m json.tool some.json

(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).

Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.

You can also use jq:

jq . some.json

and you get colors as a bonus (and way easier extendability).

Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.

🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Learn how to pretty print JSON in Python using built-in tools like json.dumps() and pprint to improve readability and debug structured data efficiently.
🌐
ReqBin
reqbin.com β€Ί code β€Ί javascript β€Ί ounkkzpp β€Ί javascript-pretty-print-json-example
How do I pretty print JSON in JavaScript?
JavaScript has a built-in JSON.stringify(obj, replacer, space) method to convert objects to JSON and pretty-print the generated JSON string.
🌐
JSON Compare
jsoncompare.org β€Ί json-pretty-print
JSON Pretty Print | free online json prettify
Online free JSON pretty print is used to view your json data in a proper format. So, you can easily understand error and json data and correct them.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - Pretty printing JSON in Python is an important skill to have for anyone working with JSON data. In this tutorial, we learned how to use the json module in Python to pretty print JSON as well as the pprint module.