Skip to content
Toolsshark

Tutorials

Fixing invalid JSON: the eight errors behind most failures

JSON is strict in ways JavaScript is not. These are the mistakes that break parsers most often, how to spot them, and how to fix each one.

Published
12 Sept 2026
Reading time
1 min
By
Toolsshark Lab

JSON looks like JavaScript object syntax, which is exactly why it goes wrong: code that is valid in a JavaScript file is often invalid JSON. Parsers stop at the first error, so knowing the common ones saves a lot of trial and error.

1. Trailing commas

{
  "name": "Toolsshark",
  "tools": 9,
}

The comma after the last property or array item is not allowed. Remove it.

2. Single quotes

Strings and keys must use double quotes. 'name' is invalid; "name" is valid.

3. Unquoted keys

Every key must be a double-quoted string, even when it is a simple word: { "id": 1 }, not { id: 1 }.

4. Comments

Standard JSON has no comments. Configuration formats such as JSONC or JSON5 allow them, but a strict parser will fail. Remove comments or move notes into a field like "_comment".

5. undefined, NaN and Infinity

Only null, true, false, numbers, strings, arrays and objects are allowed. Replace undefined and NaN with null.

6. Unescaped control characters in strings

A literal line break or tab inside a string is invalid. Escape it as \n or \t. This is common when text is pasted from a document into a JSON value.

7. A byte order mark at the start

Some editors save a UTF-8 byte order mark at the start of a file. It is invisible, but many parsers reject it with an 'unexpected token' error at position 0. Re-save the file as UTF-8 without BOM.

8. Duplicate keys

Duplicate keys are technically allowed by the specification, but behaviour is undefined — most parsers silently keep the last value. Treat duplicates as a bug even when parsing succeeds.

Keep reading

All articles