Skip to content
Utilboxes

How to fix invalid JSON: the common errors and what they mean

Trailing commas, single quotes, comments, unescaped characters and the other mistakes that make JSON invalid — with examples and how to find the exact line.

Published

Tools for this job: JSON Validator · JSON Formatter · JSON to CSV

“Unexpected token”, “Expected property name”, “Unexpected end of JSON input”. JSON error messages are terse, and in a long file they can be hard to act on. Almost every one comes from a small set of mistakes, and JSON is strict about all of them.

Find the exact position first

Paste the JSON into the JSON Validator. It checks the text as you type, and when it fails it names the likely cause in plain words and highlights the line. The JSON Formatter reports the exact line and column with a marker under the problem character. Both run in your browser, so it is safe to paste real data.

One tip: the reported position is where the parser gave up, which is sometimes just after the real mistake. If the marked line looks fine, check the end of the line before it.

The common errors

  • Trailing commas. {"a": 1, "b": 2,} is invalid. JSON allows no comma after the last item in an object or array. This is the most common error of all.
  • Single quotes. {'name': 'Ada'} is invalid. Keys and strings must use double quotes.
  • Unquoted keys. {name: "Ada"} is valid JavaScript but not JSON. Every key must be a double-quoted string.
  • Comments. // note and /* note */ are not allowed. JSON has no comment syntax.
  • Missing commas between items, often after copying and pasting a block.
  • Mismatched brackets — an object opened with a brace and closed with a square bracket, or a missing closing bracket at the end, which produces “unexpected end of input”.
  • Values JSON does not have: undefined, NaN and Infinity are not valid. Use null or a number.

Strings and escaping

Inside a string, some characters must be escaped with a backslash:

  • a double quote: \"
  • a backslash: \\ — the usual problem with Windows file paths
  • a line break: \n; a tab: \t

A real line break typed inside a string is invalid; it must be written as \n. Text pasted from a word processor can also bring curly quotes, which are ordinary characters inside a string but break the syntax if they replace the straight quotes around it.

Numbers

  • No leading zeros: 012 is invalid; write 12, or a string if the zero matters.
  • No plus sign, no hexadecimal, and no trailing decimal point.
  • Very large numbers may lose precision in some languages; long IDs are safer as strings.

JSON is not a JavaScript object

Many errors come from writing JSON as if it were JavaScript. JavaScript accepts single quotes, unquoted keys, trailing commas, comments and functions; JSON accepts none of them. Some tools use relaxed variants such as JSON5 or JSONC that allow comments and trailing commas, but a standard JSON parser — which is what most APIs use — rejects them.

Valid but probably wrong: duplicate keys

{"id": 1, "id": 2} parses, but most parsers silently keep only the last value. The validator flags duplicate keys because they are almost always a mistake and very hard to spot by eye.

Valid syntax is not the same as valid data

A validator checks that the text is well-formed JSON. It does not check that a field is the right type, that a required field exists, or that an API will accept the document — that needs a JSON Schema or the API's own documentation.

After it is valid

Format it with two-space indentation to read it comfortably, minify it for sending, or turn an array of records into a spreadsheet with JSON to CSV.

Tools used in this guide

More tools for this job

All guides