Pretty-Print JSON Online
Pretty-print and beautify JSON with proper indentation and line breaks. Make minified or compact JSON human-readable for debugging and editing.
Detailed Explanation
Pretty-printing JSON means formatting a compact or minified JSON string with consistent indentation, line breaks, and spacing so that the hierarchical structure is visually clear. This is essential for debugging, code review, and any situation where a human needs to read or edit JSON data.
Why pretty-printing matters:
Minified JSON like {"users":[{"name":"Alice","roles":["admin","editor"]},{"name":"Bob","roles":["viewer"]}]} is nearly impossible to scan visually. Pretty-printing reveals the nesting structure at a glance, making it easy to spot missing values, incorrect nesting, or unexpected data types. Most developers spend far more time reading JSON than writing it, so formatting directly impacts productivity.
How to pretty-print in JavaScript:
The built-in JSON.stringify() method accepts a third parameter for indentation:
const formatted = JSON.stringify(data, null, 2); // 2-space indent
const formatted4 = JSON.stringify(data, null, 4); // 4-space indent
const tabbed = JSON.stringify(data, null, "\t"); // tab indent
The second parameter is a replacer function or array that can filter or transform values during serialization. Passing null means no transformation.
Indentation conventions:
The two most common conventions are 2-space and 4-space indentation. Two spaces is more compact and preferred in frontend JavaScript ecosystems (matching common ESLint and Prettier defaults). Four spaces provides greater visual separation and is common in Python and Java ecosystems. Tab indentation is less common for JSON but is supported. There is no official standard — the JSON specification (RFC 8259) does not mandate any formatting.
Common mistakes developers make:
A frequent error is attempting to pretty-print a string that is not valid JSON, which causes the parser to throw an exception. Always validate before formatting. Another mistake is double-encoding: calling JSON.stringify() on a string that is already JSON text produces an escaped string rather than formatted output. You must first parse with JSON.parse(), then re-stringify with indentation. Developers also sometimes commit large pretty-printed JSON files to version control when a minified version would be more appropriate.
Best practices:
Use pretty-printing in development environments, log viewers, and debugging tools. Configure your editor or IDE to auto-format JSON files on save. For version-controlled JSON, agree on a consistent indent style across the team to minimize diff noise.
Use Case
Formatting a minified API response during debugging so you can quickly identify a missing nested field that is causing a frontend rendering error.