Start with the error message
Almost nobody arrives at a JSON formatter because they want pretty output. They arrive because a parser rejected something and the message was unhelpful. The messages are few and each one has a small number of causes, so they are worth decoding once.
| Message | What it means | What to change |
|---|---|---|
| Unexpected token < in JSON at position 0 | The text starts with HTML. A server returned an error page, a login redirect or a 404 body where JSON was expected. | Look at the raw response body before parsing it. Log the status code, not just the parse failure. |
| Unexpected token o in JSON at position 1 | The input is the string [object Object]. Something concatenated an object into a string instead of serializing it. | Call JSON.stringify(value) and send that, not "..." + value. |
| Unexpected end of JSON input | The document stops before it closes: a cut-off response, a truncated paste, or a stream that ended early. | Check the last character. If the text looks complete, the response was probably truncated by a size limit. |
| Unexpected token ' in JSON at position N | Single quotes. JSON strings use double quotes only. | Replace ' with " around keys and string values. |
| Unexpected token } or ] | A trailing comma before the closing bracket. | Remove the comma after the last member. Ordinary JavaScript allows it; JSON does not. |
| Unexpected token \uFEFF in JSON at position 0 | The invisible token is a byte order mark. Some editors and exports write one; the JSON standard says not to add it. | Save as UTF-8 without BOM. Here the BOM is reported as a BOM at position 0 rather than the input being called empty. |
The dot in position 0 matters. Positions are counted in characters from the start of the text, so position 0 is the very first character — which is why an HTML doctype and a byte order mark both land there. Position 1 is the second character, which is where the o of [object Object] sits, since the bracket is character zero.
The rules that actually get broken
JSON has six rules and almost every failure is one of them:
- Double quotes only. Keys and strings both.
{'a': 1}is not JSON. - No trailing commas. Not after the last array element, not after the last member.
- No comments.
//and/* */are not part of the format. JSON5 and the JSON-with-comments variant used by editor settings files are different formats that look similar. - Keys are strings.
{1: "a"}is not JSON;{"1": "a"}is. - No
undefined,NaNorInfinity. Usenullor a string.JSON.stringifysilently drops object properties whose value isundefined, which is how fields disappear without an error. - One top-level value. Any value is legal at the top: an object, an array, a string, a number,
true,falseornull. Older parsers required an object or an array, which is why some tools still reject a bare string.
It looks like JavaScript, but it is not
JSON was derived from JavaScript object literal syntax and then narrowed. Everything below is valid JavaScript and invalid JSON, which is why code samples copied out of a script often fail to parse:
{a: 1} // key without quotes
{'a': 1} // single quotes
{a: 1,} // trailing comma
{a: 1} // note // comment
{a: undefined} // undefined
{a: 1 + 2} // expression instead of a value
The reverse direction has no such trap: anything that is valid JSON is also a valid JavaScript expression, so a JSON document can be pasted into a script as an object literal and it will still run.
The failure that has no error message
A formatter can also change your data without complaining, and this is the one to know about. JSON has a single number type, and every implementation parses it as a double-precision float. That gives 53 bits of integer precision, so any integer beyond 9,007,199,254,740,991 (253−1) may come back different from what you sent. A 19-digit Snowflake ID or a large account number is exactly the kind of value that gets quietly rounded.
Two smaller side effects of the same rule: 1.0 comes back as 1, and 1e2 comes back as 100. Nothing is lost — the number is the same — but a byte-for-byte comparison with the input will fail. If identifiers matter, send them as strings.
Duplicate keys, and why two tools disagree
The JSON standard says names within one object should be unique, but it does not say what to do when they are not. In practice each parser keeps one and the choice is not part of the spec, so a document with two "id" members can produce different results in two tools. If a formatter shows you an object with fewer keys than the text had, look for the duplicate before blaming the tool.
Why this runs in the tab
JSON is rarely neutral data. It is the shape of API responses, and API responses carry access tokens, session identifiers, email addresses and internal hostnames. Formatting it by hand is how that text gets pasted into whichever site ranks first. Here the parse, the validation and the formatting all run in the browser: nothing is sent to a server, nothing is logged, and the file you paste is not stored between visits.
When formatting is worth it
A diff is the main reason. Two JSON files that differ by a single value are unreadable one line at a time and obvious once both are formatted with the same indentation, because the change becomes one line in the diff instead of a reshuffled block. Minifying is the opposite case: it is for payloads you are about to send or ship, where the whitespace is bytes you do not need to transmit. Two spaces versus four is a style choice; pick one and stay with it, because consistency is what makes the diff work.