DeveloperApril 2026 ยท 5 min read

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.

๐Ÿ“‹
Try the JSON Formatter
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Why Format JSON?02Formatting JSON in JavaScript03Common JSON Syntax Errors04Using the JSON Formatter Tool
โšก Key Takeaways
  • 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:

๐Ÿ’ก Tip
Use 3+ color stops instead of 2 to avoid the muddy gray band that appears in the center of complementary-color gradients.

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.

โš  Warning
CSS gradients used as backgrounds cannot be animated with standard transitions. Use 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.

Frequently Asked Questions

How do I pretty-print JSON?+
In JavaScript, use JSON.stringify(data, null, 2) for 2-space indentation. For quick formatting without code, paste the JSON into the JSON Formatter tool for instant pretty-printing with syntax validation.
Why is my JSON invalid?+
Common causes: trailing commas after the last item, single quotes instead of double quotes, unquoted property names, comments (not allowed in JSON), or missing commas between items.
What is the difference between JSON and JavaScript objects?+
JSON is a strict subset of JavaScript object notation. JSON requires double quotes on all keys and strings, doesn't allow trailing commas, doesn't support comments, functions, or undefined, and only allows strings, numbers, booleans, null, arrays, and objects as values.
Try it yourself

Use the JSON Formatter โ€” free, no signup required.

โšก Open JSON Formatter
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.