Common JSON Syntax Errors and How to Fix Them
The eight JSON syntax errors you actually hit in production — trailing commas, single quotes, BOMs, NDJSON — with concrete fixes for each.
SyntaxError: Unexpected token } in JSON at position 47 is the least
helpful error message in modern development. The parser tells you where
it gave up, not what is actually wrong. This guide walks through the
eight syntax errors that account for almost every broken-JSON ticket and
shows you exactly how to fix each one.
Why "Unexpected token" messages are unhelpful
Most JSON parsers are written as state machines that abort on the first unexpected byte. The position they report is where the parser noticed something was wrong, which is usually downstream of the actual mistake. A missing comma on line 3 surfaces as an "unexpected token" on line 4 or 5.
Two habits help:
- Always validate the entire document. Don't fix the reported line and re-run; fix it, then re-validate. The next error is often only visible after the first is gone.
- When the error position is past the end of the file (
position 4711in a 4710-character file), the document is truncated — focus on unclosed brackets and quotes, not on what looks broken.
The JSON Validator reports a human-readable cause for each error class below.
1. Trailing commas
The single most common error. JavaScript allows trailing commas; JSON does not.
{
"a": 1,
"b": 2,
}
Fix — drop the comma before the closing bracket:
{
"a": 1,
"b": 2
}
This also applies inside arrays: [1, 2, 3,] is invalid.
2. Missing or extra commas
Every value in an object or array must be separated from the next by exactly one comma:
{
"a": 1
"b": 2
}
Fix — add the missing comma after 1. The mirror image is
{"a": 1,, "b": 2}, which has one too many.
3. Single quotes and unquoted keys
JavaScript object literals are forgiving; JSON is not. All strings, including object keys, must use double quotes.
Invalid:
{ name: 'Ada', role: "admin" }
Valid:
{ "name": "Ada", "role": "admin" }
If you are pasting a JS object from a console or a stack trace, this is almost always the first thing that breaks. The JSON Repair tool will quote keys and convert single quotes for you.
4. Comments inside JSON
There are no comments in JSON. //, /* */, and # are all syntax errors.
{
// user record
"id": 42
}
Fix — remove the comment, or move it outside the JSON document (in a
sibling file, in a wrapping schema, or in a _comment key if you really
must inline it). JSONC (the format VS Code uses for tsconfig.json)
allows comments, but it is not JSON and most parsers reject it.
5. Unescaped characters in strings
Inside a JSON string, the following characters must be escaped:
"→\"\→\\- control characters (U+0000 to U+001F) — for example, a literal newline
must be written as
\n, a tab as\t.
Common error — a literal newline pasted into a string:
{ "message": "line one
line two" }
Fix:
{ "message": "line one\nline two" }
Forward slash / may optionally be escaped as \/ but does not need to be.
6. Byte-order marks and encoding issues
A UTF-8 byte-order mark (BOM) is three bytes — EF BB BF — that some
editors write at the start of a file. The JSON spec forbids a leading
BOM. Strict parsers (Node's JSON.parse, Go's encoding/json, Python's
json in some configurations) reject the file with a confusing "unexpected
token" pointing at position 0.
Symptoms — the file looks fine in your editor, but a hex view shows
non-printable bytes before the opening {.
Fix — re-save the file as UTF-8 without BOM. Most editors have this option in the encoding selector. On the command line:
sed -i '1s/^\xEF\xBB\xBF//' broken.json
7. JSONP and Python literal output
Two non-JSON formats people mistake for JSON:
- JSONP —
callback({"a":1});. This is a JavaScript function call with a JSON-shaped argument. Strip the callback wrapper and trailing semicolon to get JSON. - Python
reproutput —{'a': 1, 'b': True, 'c': None}. Single quotes, capitalisedTrue/False/None. Usejson.dumps, notprint, to emit real JSON from Python.
The JSON Repair tool handles both.
8. Truncated or concatenated JSON / NDJSON
Two failure modes:
- Truncated — the file was cut off mid-document. The parser fails at the end of input because brackets are unclosed. Check the file size against what you expected, and re-fetch if it is short.
- Concatenated — two valid JSON documents joined head-to-tail:
{"a":1}{"b":2}. This is NDJSON (or JSON Lines) if there is a newline between them, and is a common format for log streams. Parse it line-by-line, not as a single document:
for (const line of text.split("\n")) {
if (!line.trim()) continue;
const record = JSON.parse(line);
// ...
}
How to find and fix fast
The triage workflow:
- Run the document through the JSON Validator — it reports the first error and a likely cause.
- If the error is structural (trailing commas, single quotes, unquoted keys, BOM), use the JSON Repair tool — it normalizes in one step.
- If the error survives both, the document is probably truncated or actually-not-JSON (HTML error page, Python repr, JSONP). Look at the first and last 100 bytes.
Next steps
- Safely parsing JSON in JavaScript — how to handle these errors in production code.
- What is JSON? — the syntax rules these errors break.