JSON pretty printing (also called beautifying) adds whitespace, indentation, and line breaks to JSON to make it human-readable. Minified JSON saves bandwidth but is nearly impossible to read, while pretty printing solves this by formatting the data into a structured view.

How to Pretty Print JSON

  • Python: Use the built-in json.dumps() function with the indent parameter to specify spaces (e.g., json.dumps(data, indent=4)). The pprint module can also be used for Python data structures, and third-party libraries like simplejson offer additional formatting options.

  • JavaScript: Call JSON.stringify(obj, null, 2) where the third argument defines the number of spaces for indentation. You can also use JSON.stringify(obj, undefined, '\t') to use tabs instead of spaces.

  • Java: Utilize the Jackson library with ObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject) or enable SerializationFeature.INDENT_OUTPUT globally.

  • Terminal: Tools like jq or Python scripts can be used to read files or clipboard data with syntax highlighting directly in the terminal.

  • Browser Dev Tools: Paste the JSON into the console or assign it to a variable and interact with it to view the pretty-printed output without additional tools.

Online Tools

Several online platforms offer instant formatting and validation:

  • JSONFormatter.org and JSONLint provide free online JSON prettifiers that format automatically as you type.

  • JSONPretty.org and CodeBeautify offer user-friendly interfaces with features like error detection, syntax highlighting, and collapsible nodes.

  • Browser Extensions: Tools like the "JSON Pretty" Chrome extension allow for parsing, formatting, and viewing JSON data locally with light/dark themes.

๐ŸŒ
JSON Formatter
jsonformatter.org
Best JSON Formatter and JSON Validator: Online JSON Formatter
Online JSON Formatter / Beautifier and JSON Validator will format JSON data, and helps to validate, convert JSON to XML, JSON to CSV. Save and Share JSON
JSON Parser
Online JSON Parser helps to parse, view, analyze JSON data in Tree View. It's a pretty simple and easy way to parse JSON data and share it with others.
JSBeautifier
Online JS Beautifier helps to Beautify Javascript. This Javascript Beautifier helps to unminify, Save and Share Javascript.
XML Formatter
Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.
Best JSON Pretty Print Online
Best JSON Pretty Print tool to Make Pretty JSON and JSON Print in color and Save and Share Online.
๐ŸŒ
JSON Formatter
jsonformatter.curiousconcept.com
JSON Formatter & Validator
The JSON Formatter & Validator beautifies and debugs JSON data with advanced formatting and validation algorithms.
๐ŸŒ
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.
๐ŸŒ
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 Pretty
json-pretty.com
JSON Pretty โ€” simple JSON formatter and editor
JSON Pretty help you to format, edit and pretty print json data. A powerful json formatter, editor and beautify tool for easy data readability.
๐ŸŒ
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 ...
๐ŸŒ
Package Control
packagecontrol.io โ€บ packages โ€บ Pretty JSON
Pretty JSON - Packages - Package Control
To prettify JSON, make selection of json (or else it will try to use full view buffer) and through Command Palette Ctrl+Shift+P find โ€œPretty JSON: Format JSONโ€ (you can search for part of it like 'pretty format')
๐ŸŒ
Chrome Web Store
chromewebstore.google.com โ€บ detail โ€บ json-pretty โ€บ nflbalmkceonkkhifbifebanhladdgcp
JSON Pretty - Chrome Web Store
Use JSON Pretty to parse, format, and pretty print json data. A powerful json formatter and beautify tool for easy data readability.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Jsonprettyprint
jsonprettyprint.net
โœ… JSON Pretty Print ๐Ÿ˜€
JSON Pretty Print 5 stars - "JSON Pretty Print" - Steve Wozniak In all my years as a programmer I worked with a lot of APIs. Whenever I get raw JSON responses from APIs I like to use JSON Pretty Print to format JSON to make it more readable. I've tried a lot of online viewers but this one is the best by a mile because of it's simplicity.
๐ŸŒ
JSON Editor Online
jsoneditoronline.org
JSON Editor Online: edit JSON, format JSON, query JSON
Format JSON is the same as beautify JSON : you make your JSON file readable by styling it with white spacing, newlines, and indentation. In short: paste your JSON file, then click the "Format" button in code mode, or select "Copy formatted" ...
๐ŸŒ
Jsonpretty
jsonpretty.org
JsonPretty - JSON Beautifier and Formatter
Working with JSON data can be a complex task, especially when the code is poorly formatted or difficult to read. JSON Pretty is an invaluable online tool designed to solve this problem by instantly beautifying your JSON data.
๐ŸŒ
LornaJane
lornajane.net โ€บ posts โ€บ 2024 โ€บ pretty-print-json-with-jq
Pretty-print JSON with jq | LornaJane
jq is a JSON tool, and the "." part is technically a filter โ€“ but it includes everything, so itโ€™s more like a not-filter. The output of jq is the same JSON that we put in, but itโ€™s pretty-printed.
๐ŸŒ
Jam
jam.dev โ€บ utilities โ€บ json-formatter
JSON formatter | Free, Open Source & Ad-free
Beautify and structure raw JSON data with proper indentation and formatting, making it easier to read, edit, and validate JSON content.
Top answer
1 of 16
6933

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:

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; }

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.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - By default, this function produces a JSON string without any formatting, but we can use the indent parameter to specify the number of spaces to use for indentation. Here's an example of how to pretty print JSON in Python:
๐ŸŒ
Teleport
goteleport.com โ€บ resources โ€บ tools โ€บ json-prettifier
JSON Prettifier | Instantly Format & Beautify JSON | Teleport
JSON Prettify tools automatically add indentation and line breaks, transforming unformatted text into a structured and visually accessible format. This makes the JSON much easier to debug, validate, and share with other developers on your team.
๐ŸŒ
Js
pretty-print-json.js.org
Pretty-Print JSON โ€ข Interactive online JavaScript tool to format ...
Pretty-print JSON data into HTML to indent and colorize (written in functional TypeScript)
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ pretty-print a json in java
Pretty-Print a JSON in Java | Baeldung
July 15, 2023 - Always use the latest versions from the Maven central repository for gson. To pretty-print JSON, weโ€™ll use the setPrettyPrinting() method of GsonBuilder: