How to read a JSON error message
A parser stops at the first character it cannot accept. The mistake is there or just before it, often at the end of the previous line. Fix one error at a time: the next one stays hidden until the first is gone.
Each engine words it differently. The same trailing comma gives Expected double-quoted property name in JSON at position 25 in Chrome and Node.js, JSON.parse: expected double-quoted property name at line 3 column 1 in Firefox, and JSON Parse error: Property name must be a string literal in Safari. Positions count from zero; lines and columns from one.
The JSON validator gives the same message in every browser, with the line, the column and a fix.
1. Trailing comma
{
"name": "Ada",
"role": "admin",
}
After the last comma the parser expects another key and finds }. JavaScript allows this comma; JSON does not. Delete it, in objects and arrays alike. Details: Expected double-quoted property name.
2. Single quotes
{ 'name': 'Ada' }
Keys and strings take double quotes only. Swap the quotes, and escape any double quote inside the text as \".
3. Unquoted keys
{ name: "Ada" }
Valid JavaScript, invalid JSON: write { "name": "Ada" }. Chrome reports this and the single-quote case as Expected property name or ‘}’.
4. Comments
{
// the user
"name": "Ada" /* required */
}
JSON has no comments, so parsing fails at the first /. Remove them, or keep notes in a key like "_comment". Editors flag them too: VS Code says Comments are not permitted in JSON, JetBrains IDEs say JSON standard does not allow such tokens. See Can JSON have comments?
5. Missing comma or bracket
{
"a": 1
"b": 2
}
A missing comma fails on the next key with Expected ',' or '}' after property value. A missing closing bracket gives Unexpected end of JSON input, because the text runs out first. Indentation makes a missing closer easy to spot, so format the JSON as soon as it parses.
6. Unexpected end of JSON input
The text ended before the JSON did: an empty body (JSON.parse('') throws exactly this) or data cut off by a network error, a size limit or a model’s token limit. Check the response is not empty before parsing. Fixes: Unexpected end of JSON input. Python’s version is JSONDecodeError: Expecting value.
7. Unexpected token < (HTML instead of JSON)
A < at position 0 means the server sent HTML: an error page, a login redirect or the wrong URL. Check the status code and the Content-Type header. Each runtime names it differently:
- Chrome and Node.js: Unexpected token ‘<’, “<!DOCTYPE “… is not valid JSON
- Firefox: JSON.parse: unexpected character at line 1 column 1
- Safari and React Native: JSON Parse error: Unrecognized token ‘<’
- Go: invalid character ‘<’ looking for beginning of value
8. Values JSON does not have
{ "a": undefined, "b": NaN, "c": True, "d": None }
JSON’s only literals are true, false and null. undefined, NaN and Infinity come from JavaScript; True, False and None from Python. Build JSON with a serializer (JSON.stringify, json.dumps), not by hand.
Two look-alikes come from parsing something that was never a JSON string: “undefined” is not valid JSON and “[object Object]” is not valid JSON.
9. Unescaped characters inside strings
{ "path": "C:\new\folder", "note": "line one
line two" }
Inside a string, a backslash starts an escape and raw line breaks or tabs are not allowed. Write \\, \n and \t: "C:\\new\\folder". Expect Bad control character in string literal, or Unterminated string in JSON when a closing quote is missing. The JSON escape tool does the escaping for you.
10. Numbers JSON rejects
No leading zeros (007), plus signs (+5), bare decimal points (5., .5) or hex (0xFF). Write 7, 5, 5.0, 0.5 and 255. Keep ZIP codes and phone numbers as strings.
11. Extra text after the JSON
{"id": 1}
{"id": 2}
A document holds one top-level value. Two objects in a row are JSON Lines: parse them line by line, or wrap them in an array. Chrome calls this Unexpected non-whitespace character after JSON.
12. Invisible characters at the start
A byte order mark (BOM) from some editors fails at position 0. Text copied from web pages, chats and word processors can carry non-breaking spaces and curly quotes (“ ”). Re-save as UTF-8 without BOM and retype the quotes. Java’s XML parsers trip on the same BOM: Content is not allowed in prolog.
“Improperly formatted JSON”
Apps show this generic message when their parser rejects your input without saying where. Any error above can cause it. The validator finds the line and column.
JSON error codes
Some tools report a number instead of a message.
PHP: json_last_error() after json_decode() fails. Compare against the constant names, not the numbers.
| Code | Constant | Meaning |
|---|---|---|
| 0 | JSON_ERROR_NONE |
No error |
| 1 | JSON_ERROR_DEPTH |
Nested deeper than the depth limit |
| 2 | JSON_ERROR_STATE_MISMATCH |
Invalid or malformed JSON |
| 3 | JSON_ERROR_CTRL_CHAR |
Control character in a string, often an encoding problem |
| 4 | JSON_ERROR_SYNTAX |
Syntax error, the most common one |
| 5 | JSON_ERROR_UTF8 |
Malformed UTF-8 |
VS Code: the number in brackets after each JSON problem.
| Code | Message |
|---|---|
| 261 | Invalid escape character in string |
| 513 | Property expected |
| 514 | Comma expected |
| 515 | Colon expected |
| 516 | Value expected |
| 519 | Trailing comma |
| 520 | Duplicate object key |
| 521 | Comments are not permitted in JSON |
Fix most of these automatically
JSON Repair fixes trailing commas, comments, single quotes, unquoted keys, Python and JavaScript values, missing commas, truncated text and the code fences chat models add. Check the result: it can close a truncated document but not bring back lost data.
A quick checklist
- Is the text non-empty and complete, starting with
{or[? - Are all keys and strings in double quotes?
- One comma between items, none after the last?
- Do all
{ }and[ ]pairs match? - Only strings, numbers, objects, arrays,
true,falseandnull? - Are backslashes, quotes, line breaks and tabs inside strings escaped?