Skip to content
HandyKit

Common JSON Errors and How to Fix Them

· 4 min read

JSON looks like JavaScript, but its rules are much stricter. Most "invalid JSON" errors come from a handful of habits carried over from JavaScript, Python or hand-edited config files. This guide covers the errors you are most likely to meet, what the message means, and how to fix each one.

Every broken example below is shown with the exact message the JSON Validator gives for it. Paste any of them in to see the line and column it points to.

Trailing comma

JavaScript lets you leave a comma after the last item in an object or array. JSON does not.

{
  "name": "Ada",
  "role": "admin",
}

The validator reports: Trailing comma before "}".

Remove the comma after the last property:

{
  "name": "Ada",
  "role": "admin"
}

This is the most common JSON error by far, because editors and code formatters often add trailing commas to JavaScript automatically. The same rule applies to arrays: [1, 2, 3,] is invalid.

Single quotes

JSON strings and property names must use double quotes. Single quotes usually mean the text was copied from Python or JavaScript source code.

{'name': 'Ada'}

The validator reports: Property names must use double quotes.

{"name": "Ada"}

If a string contains a double quote, escape it with a backslash: "She said \"hi\"".

Unquoted property names

A JavaScript object literal can have bare property names. In JSON, every name is a string in double quotes.

{name: "Ada", active: true}

The validator reports: Property names must be wrapped in double quotes.

{"name": "Ada", "active": true}

Missing comma

Forgetting a comma between two properties is easy when you add a line to the middle of a file.

{
  "host": "localhost"
  "port": 5432
}

The validator reports: Expected "," or "}" after the property value.

The error points at "port", the start of the property after the missing comma, because that is where the parser realised something was wrong. Add the comma at the end of the line above:

{
  "host": "localhost",
  "port": 5432
}

Comments

JSON has no comment syntax. Formats such as JSONC (used by VS Code settings and tsconfig.json) and JSON5 allow comments, but a standard JSON parser rejects them.

{
  // Database settings
  "host": "localhost"
}

The validator reports: Comments are not allowed in JSON.

{
  "host": "localhost"
}

If you need to keep a note in the data, add it as an ordinary property, such as "_comment": "Database settings", which every parser accepts.

Python and JavaScript values

True, False and None are Python. undefined, NaN and Infinity are JavaScript. None of them are JSON, which only has true, false and null.

{"active": True, "manager": None}

The validator reports: "True" is not valid JSON.

{"active": true, "manager": null}

This often happens when a Python dictionary is printed with print(data) instead of being serialised with json.dumps(data). The second gives valid JSON.

NaN and Infinity have no JSON equivalent at all. Store them as null or as strings, and agree on the meaning with whoever reads the data.

Leading zeros

JSON numbers cannot start with a zero unless the number is zero itself, so values like ZIP codes and phone numbers can't be written as plain numbers.

{"zip": 02134}

The validator reports: Numbers cannot have leading zeros.

Store the value as a string, which also keeps the zero:

{"zip": "02134"}

As a rule, anything you won't do arithmetic on, such as IDs, postcodes and phone numbers, is safer as a string.

Backslashes in strings

A backslash starts an escape sequence such as \n or \", so a Windows path with single backslashes is invalid.

{"path": "C:\Users\ada"}

The validator reports: Invalid escape sequence "\U".

Double each backslash, or use forward slashes, which Windows also accepts in most places:

{"path": "C:\\Users\\ada"}

Line breaks inside strings

A JSON string must be on a single line. Pressing Enter inside the quotes, or a missing closing quote, gives this error.

{"message": "Hello
world"}

The validator reports: Line break inside a string.

Write the line break as \n:

{"message": "Hello\nworld"}

Unclosed brackets

When a bracket isn't closed, the parser either meets a closing bracket of the wrong kind, or reaches the end of the input while still waiting. In the second case the message says where the unclosed bracket was opened, which is usually more useful than where the input ended.

{
  "tags": ["json", "api"
}

The validator reports: Expected "," or "]" after the array item.

Here the ] is missing, so the parser finds } where it expected the array to continue. Close the array:

{
  "tags": ["json", "api"]
}

More than one value

A JSON document holds exactly one value. Two objects one after another, as in some log files, are not one JSON document.

{"id": 1}
{"id": 2}

The validator reports: Unexpected data after the end of the JSON value.

Wrap the values in an array:

[
  {"id": 1},
  {"id": 2}
]

Files with one JSON value per line have their own name, JSON Lines (.jsonl). Tools that expect JSON Lines read them line by line, but a standard JSON parser does not.

Finding errors quickly

A parser stops at the first error, so fix errors one at a time, from the top. When an error seems to be in the wrong place, look just before it: a missing comma or quote is usually on the line above.

If the JSON came from code or a config file and has several of these problems at once, the JSON Formatter's Repair button fixes trailing commas, quotes, comments, Python values and more in one step, and lists each change so you can check it.

Tools in this guide

  • JSON Validator

    Paste or open JSON to check it as you type. Errors show their line, column and a suggested fix; valid JSON gets a summary of its structure.

  • JSON Formatter

    Paste or open JSON to pretty-print it, explore it as a tree, or repair almost-JSON. Errors are pinpointed by line and column, with a suggested fix.

  • · 2 min read

    Minify JSON: What It Does and When to Use It

    What minifying JSON removes, what it never changes, when it is worth doing and when it is not, and why sorting keys gives stable output you can compare or hash.

  • · 2 min read

    How to Compare Two JSON Files

    Find what changed between two JSON documents, ignoring key order and formatting. A worked example, how to read JSONPath, and how arrays and numbers compare.

  • · 2 min read

    How to Convert JSON to CSV, YAML or XML

    Turn JSON into a CSV spreadsheet, YAML or XML, with worked examples showing how nested objects, arrays and null values are handled, and how to open it in Excel.