Common JSON syntax errors and how to fix them
The JSON mistakes that cause parse errors, from trailing commas to bad backslashes, each shown with a broken example and the fix.
By the tools4free.dev team · Last reviewed 4 December 2026
JSON is stricter than JavaScript object syntax. Almost every "Unexpected token" or "invalid JSON" error comes from one of about a dozen causes: a trailing comma, single quotes, an unquoted key, a comment, a stray control character or a bad backslash. This guide lists each cause with a broken example and the fix. Every example was run through JSON.parse in Node.js to confirm that it fails (or, in one case, that it silently misbehaves).
The rules in brief
JSON, defined by RFC 8259 and ECMA-404, has very few rules, and the parser enforces all of them:
- A document is one value: an object, array, string, number,
true,falseornull. - Strings and object keys use double quotes only.
- Items are separated by commas, with no trailing comma.
- Numbers are decimal, with no leading
+, no leading zeros and noNaNorInfinity. - There are no comments and no
undefined.
Error messages differ between languages and even between versions of the same engine, so read the position number they give. It usually points at or just after the problem.
Error reference
| Broken JSON | Problem | Fix |
|---|---|---|
{"a":1,} | Trailing comma | {"a":1} |
{'a':1} | Single quotes | {"a":1} |
{a:1} | Unquoted key | {"a":1} |
{"a":undefined} | undefined is not a JSON value | Use null or omit the key |
{"a":NaN} | NaN and Infinity are not allowed | Use null or a string |
{"a":007} | Leading zero | {"a":7}, or a string for IDs like "007" |
{"a":+1} | Leading plus sign | {"a":1} |
{"a":.5} | Number must start with a digit | {"a":0.5} |
[1.] | Digit required after the point | [1.0] or [1] |
{"a":1 "b":2} | Missing comma | {"a":1,"b":2} |
{"a":[1,2} | Mismatched bracket | {"a":[1,2]} |
{"a":"say "hi""} | Unescaped inner quotes | {"a":"say \"hi\""} |
[True] | Literals are lower case | [true] |
{"a":1}{"b":2} | Two values in one document | Wrap in an array, or use one value per line (JSON Lines) |
Worked example 1: a config file that "looks fine"
A developer copies a settings object from JavaScript source into a config.json file:
{
// database settings
host: 'localhost',
"port": 5432,
"retries": 03,
"tags": ["web", "api",],
}
A parser reports the first error it meets and stops, so you fix one problem at a time. Here there are five. The comment on line 2 is not allowed. The key host needs double quotes, and so does its value. The number 03 has a leading zero. The array has a trailing comma, and so does the object. The corrected file is:
{
"host": "localhost",
"port": 5432,
"retries": 3,
"tags": ["web", "api"]
}
If you need comments in configuration, JSON is the wrong format. Use a format that supports them, such as YAML or TOML, or a documented superset such as JSON5, and make sure your tools read it. Putting a field like "_comment" in the data works but pollutes it.
Control characters and backslashes
Inside a string, JSON forbids raw control characters, meaning anything below U+0020. A literal newline or tab typed into a string value makes the parser fail. Write them as the escape sequences \n and \t. The permitted escapes are \" \\ \/ \b \f \n \r \t and \uXXXX. Anything else, such as \U or \d, is an error.
Worked example 2: Windows paths
Windows paths contain backslashes, and that causes two different failures:
{"path": "C:\Users\sam"} // error: \U is not a valid escape
{"path": "C:\temp\new"} // parses, but silently wrong
The first one fails loudly. The second is worse: \t and \n are valid escapes, so the parser accepts the text and produces a string containing a tab and a newline instead of "C:\temp\new". The fix is to double every backslash, or use forward slashes, which Windows APIs accept:
{"path": "C:\\Users\\sam"}
{"path": "C:\\temp\\new"}
{"path": "C:/temp/new"}
The best protection is never to build JSON by hand. In code, serialise with JSON.stringify (or your language's equivalent), which escapes everything correctly.
Invisible problems
- Byte order mark (BOM). Some editors save UTF-8 files with a leading U+FEFF. RFC 8259 says parsers may ignore it, but many reject it with an error at position 0 that points at a character you cannot see. Save as UTF-8 without BOM.
- Smart quotes. Text pasted from word processors or chat tools can contain curly quotes, which are not the ASCII double quote JSON requires.
- Non-breaking spaces. A U+00A0 character looks like a space but is not JSON whitespace. Only space, tab, line feed and carriage return are.
- Truncated input. "Unexpected end of JSON input" almost always means the file or response was cut off, or the body was empty.
Things that parse but surprise you
- Duplicate keys. The specification says names "SHOULD be unique" but does not forbid duplicates. In JavaScript,
JSON.parse('{"a":1,"a":2}')keeps the last one, giving{ a: 2 }. Other parsers may keep the first or raise an error. - Large integers. JSON itself has no size limit, but JavaScript numbers are 64-bit floats.
JSON.parse('12345678901234567890')becomes12345678901234567000. Send large identifiers as strings. - Serialising unsupported values.
JSON.stringifydrops keys whose value isundefinedor a function, turnsNaNintonull, and turns aDateinto an ISO string. The output is valid JSON but not what you started with.
A quick debugging routine
When a large document will not parse, work in small steps. First, confirm the first non-space character is { or [ and not <. Second, check the length: an empty or truncated body is common. Third, run the text through a validator that reports a line and column. Fourth, fix the first error only and run the validator again, because later errors are sometimes side effects of earlier ones. On the command line, node -e "JSON.parse(require('fs').readFileSync('config.json','utf8'))" or python3 -m json.tool config.json will each print the position of the first problem. When the data comes from your own code, log the raw string before parsing it, since the bug is usually in how it was built.
Common mistakes
- Fixing the wrong line. The reported position is where the parser gave up. A missing comma or quote earlier in the document often causes it. Look at the line before.
- Editing JSON as if it were JavaScript. Object literals in code allow trailing commas, comments and unquoted keys. JSON does not.
- Assuming the server sent JSON. An HTML error page starting with
<produces "Unexpected token <". Check the HTTP status and theContent-Typeheader before parsing. - Double-encoding. A JSON string that contains JSON, such as
"{\"a\":1}", needs a second parse. If you see lots of backslashes before quotes, you have serialised twice. - Pasting several objects. Log files with one object per line are JSON Lines, not a single JSON document. Parse them line by line.
Frequently asked questions
Can JSON have comments?
No. The standard has no comment syntax, and a conforming parser rejects them. Remove them, or choose a format that supports comments.
Why does "Unexpected token <" appear?
The text you parsed begins with <, so it is probably HTML, such as an error page or a login redirect, not JSON.
Are single quotes ever valid?
Not in JSON. They are valid in JavaScript source and in supersets such as JSON5, but not in data that a standard parser reads.
Does JSON allow a top-level value other than an object?
Yes. Since RFC 7159 and now RFC 8259, a document can be any value, so 42, "hi" and [1,2] are all valid. Very old parsers accepted only objects and arrays.
Try it: paste broken JSON into the JSON formatter to validate it, find the line of the first error and pretty-print the result once it is fixed.