Why Your JSON Is Invalid: The Seven Most Common Mistakes

JSON looks simple, which is why small slips are so easy to make and so annoying to find. Nearly every "unexpected token" message comes from one of the seven mistakes below.

Developers · Updated October 21, 2026

1. A trailing comma

In JSON there must be no comma after the last item. This is invalid: {"name": "Ana", "age": 30,}. Remove the final comma and it works. Many programming languages allow trailing commas, so this error is common when you copy a data structure out of code.

2. Single quotes instead of double quotes

JSON requires double quotes around keys and text values. {'name': 'Ana'} is not valid, while {"name": "Ana"} is. Replace every single quote that wraps a string, but leave apostrophes inside text, such as "it's", alone.

3. Unquoted keys

In JavaScript you can write {name: "Ana"}. In JSON the key must be in quotes: {"name": "Ana"}. If you copied an object literal from code, wrap every key.

4. Comments

JSON has no comment syntax. Lines that start with // or blocks inside /* */ will make the whole file invalid. If you need notes, add a field such as "_comment", or keep the notes in a separate file.

5. Missing or extra brackets

Every opening brace or bracket needs a closing one. When the error points to the very end of the text, a bracket was usually left open earlier. A formatter that indents the data makes this easy to see, because the nesting will not line up.

6. Values that JSON does not support

  • undefined, NaN and Infinity are not valid values. Use null or a string.
  • Dates are not a special type. Write them as text, such as "2026-10-21".
  • Numbers cannot have leading zeros or a leading plus sign. Write 7, not 007.
  • Hex numbers such as 0xFF are not allowed.

7. Invisible characters

Text copied from documents or web pages can contain curly quotes, which look like quotes but are different characters, and sometimes a hidden byte order mark at the start. If everything looks right and the data is still rejected, retype the quotes by hand or paste the text into a plain text editor first.

A fast way to find the error

  1. Paste the JSON into a validator that reports the line and column.
  2. Go to the line shown, and also check the line before it, because the parser often notices the problem a little late.
  3. Fix one error at a time and validate again. One mistake can cause several messages that vanish together.
  4. Once it is valid, format it so that it is easy to read and review.

Preventing errors in the first place

  • Generate JSON with code, using a library function such as JSON.stringify in JavaScript or json_encode in PHP, instead of building strings by hand.
  • Use an editor that highlights JSON and underlines mistakes as you type.
  • Validate data at the point where it enters your program, so that a bad file fails early with a clear message.
  • Keep configuration files small and formatted the same way, so that changes are easy to review.

If you receive JSON from another system and it keeps failing, look at the raw response rather than the pretty version your browser shows. Sometimes an error message in HTML, or an extra line before the data, is the real problem.

JSON, JSON5 and JavaScript objects

Some tools accept a relaxed format, often called JSON5, that allows comments, trailing commas and single quotes. That is why a file may work in one program and fail in another. Standard JSON, the version used by web services and most programming languages, does not allow any of these. When data must be exchanged between systems, stick to strict JSON and your files will work everywhere.

Try the tools

More guides