How the JSON Formatter Validates and Pretty-Prints JSON
A JSON formatter is really two passes: a strict parse that rejects invalid JSON, then a reserialization with two-space indentation. Here's exactly what happens between paste and output.
In brief
- What is it?
- DataFormatter's JSON Formatter is a browser-only tool that validates JSON with a native JSON.parse and then reserializes the parsed value with clean two-space indentation.
- Who is it for?
- Developers who paste minified or pasted-down API responses, configs and fixtures and need them readable before editing or debugging.
- How DataFormatter's tool is different
- Error positions are derived from the native parser's message and turned into 1-based line and column numbers, and everything runs locally — nothing you paste is uploaded.
A JSON formatter looks like it just adds newlines and spaces, but the interesting part happens before any indentation is applied. The pipeline is only three steps: trim the input, parse it, and if the parse succeeds reserialize the parsed value. Parse failure never reaches the formatting step — you get an error with a precise location instead.
Two passes under the hood
The first pass is validation, and it uses the browser's own JSON.parse. Because JSON.parse implements the JSON grammar (RFC 8259), it is deliberately strict: keys must be double-quoted, strings must use double quotes, there are no comments, no single quotes, no trailing commas, and bare words like NaN, Infinity or undefined are rejected. The formatter never tries to 'fix' the input — invalid JSON fails loudly rather than being silently interpreted.
{ // ← no comments allowed
"valid": true,
"trailing": 1, // ← no trailing commas
'singles': "no", // ← single quotes are not strings
}Pinpointing the exact error
When JSON.parse throws, its message tells you where parsing stopped — for example 'Unexpected token } in JSON at position 14'. The formatter reads that message with a small parser of its own: it matches /position (\d+)/ to get the character offset, then slices the input up to that offset and counts newlines to convert it into a 1-based line number and a column. For single-line inputs it also understands the 'line X column Y' form directly. The noisy 'in JSON at position N' suffix is stripped from the message so you see the actual problem.
{"name": "first",
"age": }Unexpected token } in JSON at position 17
→ line 2, column 9The format pass
Once the input is valid, formatting is a single call: JSON.stringify(parsedValue, null, 2). The third argument (2) is the indentation level — two spaces per nesting level, which is the web convention you see in API docs and most linters. Because the pretty output is produced from the parsed value rather than by editing the raw string, the two spaces are applied consistently even after numbers, big strings or deeply nested arrays.
What is and is not valid JSON
- Object keys must be quoted strings: { "ok": true }, never { ok: true }.
- Strings use double quotes only; escapes like \n and \uXXXX work.
- No trailing commas, no comments, and no control characters inside strings.
- Numbers can be negative, exponential and fractional, but leading zeros are invalid.
- A document may be any single JSON value: object, array, string, number, true, false or null.
Format, validate or minify?
| Tool | What it does | Pick it when |
|---|---|---|
| JSON Formatter | Validates and pretty-prints with two-space indentation | You want readable output from minified or pasted-down JSON |
| JSON Validator | Parses and reports the exact line and column of errors | Only syntax matters — you want a definitive error location |
| JSON Minifier | Removes whitespace to collapse JSON to one line | Payloads must shrink for storage, logs or transfer limits |
Try it
Paste any JSON into the JSON Formatter and the position of the first error, if any, is reported with line and column. Formatting, validating and minifying all run in your browser.
Frequently asked questions
Is it safe to paste production JSON?
Yes — parsing and formatting run entirely in your browser. Nothing you paste is uploaded to a server.
Does the formatter accept comments or trailing commas?
No. Input must be strict RFC 8259 JSON. Comments, single quotes, bare identifiers and trailing commas are reported as syntax errors with a line and column.
Can the formatter minify JSON too?
No — that is the JSON Minifier tool. The formatter always reserializes with two-space indentation.