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 theindentparameter to specify spaces (e.g.,json.dumps(data, indent=4)). Thepprintmodule can also be used for Python data structures, and third-party libraries likesimplejsonoffer additional formatting options.JavaScript: Call
JSON.stringify(obj, null, 2)where the third argument defines the number of spaces for indentation. You can also useJSON.stringify(obj, undefined, '\t')to use tabs instead of spaces.Java: Utilize the Jackson library with
ObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject)or enableSerializationFeature.INDENT_OUTPUTglobally.Terminal: Tools like
jqor 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.
Videos
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, '&').replace(/</g, '<').replace(/>/g, '>');
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, '&').replace(/</g, '<').replace(/>/g, '>');
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; }
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.