How to Format and Pretty-Print JSON
Minified JSON is compact for transmission but impossible to read. Pretty-printing adds whitespace and indentation that makes the structure visible โ nested objects become clear, missing commas become obvious, and debugging API responses goes from painful to trivial.
- Format minified JSON into readable, indented output.
- Why Format JSON?.
- Covers formatting json in javascript.
- Covers common json syntax errors.
- Covers using the json formatter tool.
Why Format JSON?
API responses, configuration files, and database exports often arrive as a single line of minified JSON. Finding a specific field in a 10,000-character string is impractical. Formatting reveals the hierarchy: objects nest visibly, arrays align vertically, and structural errors become immediately apparent. Formatted JSON is also easier to diff โ comparing two formatted JSON files shows exactly which values changed.
Formatting JSON in JavaScript
JavaScript has a built-in JSON formatter:
const formatted = JSON.stringify(data, null, 2);
The third argument (2) is the number of spaces per indent level. Use 2 for compact formatting or 4 for more spread-out readability. The second argument (null) is a replacer function โ use it to filter or transform values during formatting.
To format a JSON string: JSON.stringify(JSON.parse(jsonString), null, 2)
Common JSON Syntax Errors
Trailing commas are the most common error โ JSON doesn't allow them (unlike JavaScript). Single quotes aren't valid โ JSON requires double quotes for strings and property names. Unquoted property names fail โ every key must be a string in double quotes. Comments aren't supported โ remove // and /* */ before parsing. NaN and Infinity aren't valid JSON values โ use null or a string representation instead.
background-size animation or @property registered custom properties instead.Using the JSON Formatter Tool
Paste any JSON into the JSON Formatter โ it instantly formats, validates, and highlights syntax errors with line numbers. The tool handles large payloads (tested up to 10MB), preserves Unicode characters, and lets you switch between 2-space and 4-space indentation. For API debugging workflows, you can also collapse/expand nested objects to focus on the section you're investigating.