Home › Guides › Developer › Common JSON syntax errors and how to fix them

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:

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 JSONProblemFix
{"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 valueUse null or omit the key
{"a":NaN}NaN and Infinity are not allowedUse 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 documentWrap 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

Things that parse but surprise you

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

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.

Sources