Robince Studio
JSON & Data Tools

Common JSON errors and how to fix them

JSON looks like a JavaScript object, but it is much stricter, and a single stray comma makes the whole document invalid. This guide lists the mistakes that cause most "Unexpected token" errors, with the actual messages a parser gives, so you can recognise and fix them quickly.

3 min read Reviewed Published by Abdul Gani, Robince Studio

On this page
  1. The rules JSON actually enforces
  2. Ten common mistakes, with the real error messages
  3. How to find the error fast
  4. Things that do not survive a round trip
  5. Tips to avoid errors
  6. Frequently asked questions
  7. Try these tools

The rules JSON actually enforces

JSON allows six kinds of value: objects in curly braces, arrays in square brackets, strings in double quotes, numbers, true and false, and null. Keys must be strings in double quotes. Values are separated by commas, and there must be no comma after the last item.

Anything else, including comments, single quotes, unquoted keys, undefined, NaN, Infinity and trailing commas, is invalid JSON even though many of these work in JavaScript source code. That difference is the source of most confusion.

Ten common mistakes, with the real error messages

The messages below are from the JSON parser in Node.js. Other parsers word them differently, but the cause is the same.

Invalid JSONWhat is wrongTypical error
{'a': 1}Single quotes around the keyExpected property name or "}" in JSON at position 1
{"a": 1,}Trailing comma in an objectExpected double-quoted property name at position 7
[1, 2, 3,]Trailing comma in an arrayUnexpected token "]"
{a: 1}Unquoted keyExpected property name or "}" at position 1
{"a": 1 "b": 2}Missing comma between pairsExpected "," or "}" after property value at position 7
{"a": undefined}undefined is not a JSON valueUnexpected token "u"
{"a": NaN}NaN and Infinity are not allowedUnexpected token "N"
{"a": 01}Leading zero in a numberUnexpected number at position 6
// note then {"a": 1}Comments are not allowedUnexpected token "/"
a string with a real line break inside quotesControl characters must be escaped as \nBad control character in string literal

How to find the error fast

  1. Read the position. Most parsers report a character offset or a line and column. Look just before it: the real mistake is often the previous character, such as a missing comma at the end of the line above.
  2. Paste the document into a JSON formatter. It highlights the first invalid spot and, once valid, pretty-prints the structure so you can see nesting problems.
  3. Check brackets in pairs. Every opening brace or bracket must be closed in the reverse order. Collapsing sections in an editor helps.
  4. Look at quotes. Curly "smart quotes" copied from a document or chat are not valid JSON quotes, even though they look similar.
  5. Check for hidden characters, such as a byte-order mark at the start of a file or a non-breaking space.

Things that do not survive a round trip

Converting a JavaScript object to JSON drops or changes some values. With JSON.stringify, undefined values and functions are omitted from objects and become null inside arrays, NaN and Infinity become null, and a Date becomes an ISO text string such as "1970-01-01T00:00:00.000Z".

When you read that data back you get strings, not Date objects, so convert them yourself. If a value must survive exactly, store it in a format that supports it, or encode it as a string on purpose.

Tips to avoid errors

  • Generate JSON with a library (JSON.stringify, json.dumps) instead of building strings by hand.
  • Validate incoming JSON in your application and handle parse failures with try/catch rather than crashing.
  • If you need comments or trailing commas for configuration files, use a format designed for it, such as JSON5 or YAML, and convert to strict JSON when needed.
  • Format and minify with a tool. Pretty-printed JSON is easier to read; minified JSON is smaller to send.

Frequently asked questions

Why does my JSON work in JavaScript but not in a parser?

JavaScript object literals allow single quotes, unquoted keys, trailing commas and comments. JSON does not. A snippet that runs as code can still be invalid JSON.

Can JSON have comments?

No. The standard has no comment syntax. Some tools accept them as an extension, but a strict parser will reject them.

What is the difference between JSON and a JavaScript object?

JSON is a text format for exchanging data. A JavaScript object is an in-memory value. JSON.parse turns text into an object and JSON.stringify turns an object into text.

How large can a JSON file be?

The format has no limit, but parsers load it into memory. Very large files are better handled with a streaming parser or a line-based format.

Try these tools

Keep reading