How to Format JSON: Pretty-Print, Minify and Find the Exact Error

Every developer knows the moment: an API returns a wall of JSON on one line — no indentation, no line breaks — and somewhere inside it is the one field you need, or the one syntax error breaking your app. This guide explains how to format, minify and validate JSON with our JSON formatter — pretty-printing with clean 2-space indentation, exact line-and-column error locations when the JSON is broken, and the three stats that tell you how big and how complex a payload really is. Everything runs in your browser, so even API responses containing tokens or customer records never leave the page.

How to Format JSON, Step by Step

1. Paste your JSON into the input box — straight from an API response, a config file or your editor. Not sure what to try? Hit Load sample for a realistic example object. 2. Choose an action. Format JSON pretty-prints with 2-space indentation; Minify strips all unnecessary whitespace for production payloads; Validate checks correctness and shows only the stats; Clear resets everything. 3. Read the results. Valid JSON gets a summary — total key count, maximum nesting depth and size in bytes — plus the transformed output in a scrollable code block. Invalid JSON gets the offending line highlighted with a caret pointing at the exact column, so you fix it in seconds. 4. Copy or share the output. The output area has Copy and Share buttons built in, so moving formatted JSON back into your editor or a chat thread is one click. Because validation runs on every change, you can also paste first and fix live: correct the highlighted line and the error display updates as you type until the JSON turns valid.

How Validation and Formatting Actually Work

Under the hood, the tool uses your browser's native JSON.parse — the same strict parser your JavaScript code uses — so “valid here” means “valid in your app.” Formatting is JSON.stringify with 2-space indentation, which normalizes spacing and number representation into one canonical, readable layout while preserving the original key order. Minifying is the same call with no indentation, producing the smallest correct representation. Error pinpointing works by reading the parser's message: modern engines report failures like “Unexpected token } in JSON at position 42,” and the tool extracts that position, walks the input to find the corresponding line and column, and renders the surrounding lines with the broken one highlighted and a ^ caret under the exact character. For “unexpected end of input” — truncated JSON — it points at the end of the text, which is where the missing bracket or quote belongs. The stats come from walking the parsed structure once: every object key is counted including nested ones, the deepest nesting level becomes the max depth, and the size is measured in UTF-8 bytes — the number that actually matters for payload budgets. The same browser engine powers the neighbouring developer tools, such as our base64 encoder for the encoded strings that so often travel inside JSON payloads.

Worked Example: The Sample Payload

Click Load sample and the tool fills in this object, deliberately shown unformatted: {"name": "Ada Lovelace", "role": "Developer", "active": true, "score": 98.6, "tags": ["json","api","tools"], "address": {"city": "London","zip": "E1 6AN"}, "notes": null}. Press Format and 2-space indentation turns it into nine readable lines, with the nested address object clearly visible. The stats report 8 keys, a max depth of 2 (root → address) and the size in bytes. Press Minify and the whitespace disappears again, ready to paste into a request body. Now break it: delete the closing brace and the tool reports invalid JSON, with the caret at the end of the last line — exactly where the brace belongs. The depth-2 reading is the useful one: it tells you at a glance that mapping this payload in code needs at most two dots of access, as in obj.address.city. Payloads deeper than 4 or 5 levels are usually a sign the API response could be flattened, and the depth stat makes that visible instantly. When the payload in front of you is not JSON data but a token, switch to our JWT decoder, which decodes the header and payload of a JSON Web Token and inspects its expiry.

Format or Minify? A Short Decision Rule

The data is identical either way — only the whitespace changes. Format while you are debugging, reviewing a response, writing documentation or learning an unfamiliar API: indentation is what lets your eyes follow nesting, spot the missing field and compare two payloads. Minify when you are shipping: embedding JSON in a size-sensitive place, storing payloads you will never read by hand, or shaving bytes off responses at scale. And validate before either — a formatter that silently “fixed” broken JSON would be hiding the bug you came to find, which is why this tool reports the exact error location instead of guessing.

Common Mistakes

Trailing commas. A comma after the last item in an object or array is legal in JavaScript literals and JSON5, but strict JSON (RFC 8259) forbids it — it is the single most common validation failure. Single quotes and unquoted keys. JSON strings and keys require double quotes; {'name': 'Ada'} is a JavaScript object, not JSON. Special values. NaN, Infinity and undefined have no JSON representation; APIs that emit them are producing JSON-like text your app will also reject. Assuming validation means schema validation. This tool confirms syntax, not shape — it cannot tell you a required field is missing or a value has the wrong type; that is a job for JSON Schema. Duplicate keys. The spec tolerates them but the parser keeps only the last value, while the key counter counts each occurrence — treat duplicates as a bug in your data, not a feature. Pasting secrets into online server-side formatters. This formatter never uploads your input, which is precisely why it is safe for tokens and customer records; check that property before pasting sensitive payloads anywhere else.

Frequently Asked Questions

How do I format JSON online?
Paste your JSON into the input box and click Format JSON — the tool pretty-prints it with 2-space indentation and shows the result in a scrollable code block with a Copy button. If the JSON is invalid, you will see the exact line and column of the error instead, so you can fix it and re-format.
What is the difference between formatting and minifying JSON?
Formatting (pretty-printing) adds indentation and line breaks so humans can read the structure; minifying removes all unnecessary whitespace so machines transfer fewer bytes. The data is identical either way. Format while you are debugging, minify when you are shipping payloads or embedding JSON in size-sensitive places.
How do I find the exact location of a JSON error?
Paste the broken JSON and the tool highlights the offending line and places a caret (^) under the exact character the parser choked on, with the line and column numbers shown above. The most common culprits are a missing comma between properties, a missing closing bracket or brace, and an unescaped quote inside a string.
Why does JSON validation fail on single quotes or trailing commas?
Because strict JSON only allows double-quoted strings and forbids trailing commas — single quotes and trailing commas belong to JavaScript object literals and formats like JSON5, not to JSON itself. Replace single quotes with double quotes and remove the comma after the last item in each object or array, and the JSON will validate.
What do the key count and max depth stats tell me?
Key count is the total number of properties across the whole structure, including nested objects — a quick measure of how much data you are dealing with. Max depth is the deepest nesting level (a flat object is depth 1); it tells you how many levels of access like obj.address.city your code will need, and depths beyond 4 or 5 often signal a payload worth flattening.

More guides from ToolNest

What Time Should I Go to Bed? Sleep Cycles, the 90-Minute Rule and Wake Times That Work

Bedtime counted back in 90-minute cycles — for a 6:00 AM alarm, 8:45 PM, 10:15 PM or 11:45 PM — and why 7.5 aligned hours can beat 8 misaligned ones.

How to Calculate Your Due Date: Naegele's Rule, Trimesters and the 280-Day Count

Naegele's rule worked in full — 1 January 2026 + 280 days = 8 October 2026, with conception and trimester dates and why only about 5% of babies arrive on the day.

How to Calculate Profit Margin: Margin vs Markup With Worked Examples

Cost vs selling price divided two ways — $80 to $100 is a 20% margin and a 25% markup — plus the $1,428.57 price a true 30% margin requires.