ENRU

JSON errors: messages, causes and fixes

Updated

A parser’s error message tells you where it gave up, rarely why. This reference maps the exact messages of Chrome, Firefox, Safari, Python, Go, Java, C#, PHP and other parsers to the cause behind them, with one page per cause: what the error means, the exact message of each parser, and how to fix it in code.

“Invalid” here means invalid by RFC 8259, the JSON standard. Each cause has a code, the same one the JSON validator reports with its line and column. Found your message below? The page next to it explains it.

Commas, colons and quotes

Error messageCodePage
Expected double-quoted property name in JSON (Chrome), Illegal trailing comma before end of object (Python 3.13+)trailing_commaTrailing comma
Expected ',' or '}' after property value in JSON (Chrome), Expecting ',' delimiter (Python), Unexpected string in JSON (Node.js 18)missing_commaMissing comma
Expected ':' after property name in JSON (Chrome), Expecting ':' delimiter (Python), invalid character '1' after object key (Go)missing_colonMissing colon
Expecting property name enclosed in double quotes (Python), Single quotes (') are not allowed in JSON (Safari)single_quotesSingle quotes
Unexpected token '“' (Chrome), Unrecognized token '“' (Safari)smart_quotesSmart quotes
Expected property name or '}' in JSON (Chrome), invalid character 'n' looking for beginning of object key string (Go)unquoted_keyUnquoted key

Brackets and strings that never close

Error messageCodePage
Unexpected end of JSON input (Chrome, Go), Expected '}' (Safari), EOF while parsing an object (Rust)unclosed_objectUnclosed bracket: object
end of data when ',' or ']' was expected (Firefox), EOF while parsing a list (Rust)unclosed_arrayUnclosed bracket: array
Expected ',' or ']' after array element for [1} (Chrome), State mismatch (invalid or malformed JSON) (PHP)mismatched_bracketMismatched bracket
Unterminated string in JSON at position (Chrome), Unterminated string starting at (Python)unclosed_stringUnclosed string

Things JSON doesn’t have

Error messageCodePage
Unexpected token '/' (Chrome), Comments are not permitted in JSON. (Visual Studio Code)commentComments
Unexpected token 'N', "[NaN]" is not valid JSON (Chrome), Unexpected token 'T' for True (Chrome)invalid_literalInvalid literal
Unexpected number in JSON for 01, Unexpected token '.' for .5 (Chrome)invalid_numberInvalid number
Bad escaped character in JSON (Chrome), Invalid \escape (Python)invalid_escapeInvalid escape
Bad control character in string literal in JSON (Chrome), Invalid control character at (Python)control_characterControl character

The start and the end of the document

Error messageCodePage
Unexpected end of JSON input on empty input (Chrome), Expecting value: line 1 column 1 (char 0) (Python)empty_inputEmpty input
Unexpected UTF-8 BOM (decode using utf-8-sig) (Python), invalid character 'ï' looking for beginning of value (Go)bomByte order mark
Unexpected non-whitespace character after JSON (Chrome), Extra data (Python), invalid character '{' after top-level value (Go)extra_dataExtra data
Maximum stack depth exceeded (PHP)too_deepNesting too deep
Unexpected token '<', "<html>" is not valid JSON (Chrome), invalid character '<' looking for beginning of value (Go)unexpected_characterUnexpected character

Messages with more than one cause

Some messages are not about JSON syntax at all: the server sent HTML, the response was empty, a value was parsed twice. These pages start from the message and lead to the cause.

Error messageCodePage
Unexpected token '<', "<!DOCTYPE "… is not valid JSON, Unexpected token < in JSON at position 0usually unexpected_characterUnexpected token '<': HTML instead of JSON
Unexpected end of JSON input, unexpected end of JSON input (Go)empty_input or an unclosed bracketUnexpected end of JSON input
"undefined" is not valid JSON, "[object Object]" is not valid JSON, Unexpected token u in JSON at position 0invalid_literal“undefined” is not valid JSON
Expecting value: line 1 column 1 (char 0) (Python)empty_input, unexpected_character and othersExpecting value
Expecting property name enclosed in double quotes (Python)single_quotes, trailing_comma, unquoted_key, commentExpecting property name

The most common JSON error messages

How to read a JSON error message

  1. Find the position. Chrome gives position N counted from 0, plus line L column C counted from 1; Python gives line L column C (char N); Go gives a byte offset in SyntaxError.Offset; System.Text.Json gives LineNumber and BytePositionInLine, both from 0. The position finder turns any of them into a place in the text.
  2. Look just before it. The position is where the parser gave up. A missing comma is reported at the next value, a trailing comma at the bracket after it, an unclosed bracket at the very end of the input.
  3. Fix one error at a time. The first error can produce the next ones; after a fix, parse again.
  4. Check what you parsed. If the error is at line 1 column 1, the text probably isn’t JSON at all: an empty response, an HTML page, the string undefined.

JSON errors FAQ

Why do Chrome and Python give different messages for the same JSON?

Each parser describes what it expected at the point where it stopped, in its own words. The cause is the same; the tables on each page here list the exact message of every major parser side by side.

Why does the error position point after the real mistake?

A parser only knows something is wrong when it reads a character that can’t follow. For a trailing comma that is the bracket after it; for a missing comma, the next value; for an unclosed bracket, the end of the input.

What is the difference between position, char and column?

Chrome’s position and Python’s char count characters from 0. Columns count from 1 within a line. Go, serde and System.Text.Json count bytes, so any character outside basic Latin before the error shifts their numbers.