developer · · Updated
Common JSON Errors and What the Error Messages Mean
By Chirag
When JSON fails to parse, the error message is the best clue you have, but each browser words it differently, and the position it reports is where the parser gave up, which is not always where the mistake is. To see what that looks like in practice, the same twelve broken snippets were run through JSON.parse in Chrome 153, Firefox 153 and WebKit 26.5 (the engine inside Safari). The messages below are copied exactly as each engine produced them.
The messages, side by side
| Mistake | Input | Chrome | Firefox | Safari (WebKit) |
|---|---|---|---|---|
| Trailing comma | {"name": "Asha", "age": 30,} | Expected double-quoted property name in JSON at position 27 (line 1 column 28) | expected double-quoted property name at line 1 column 28 | Property name must be a string literal |
| Single quotes | {'name': 'Asha'} | Expected property name or '}' in JSON at position 1 (line 1 column 2) | expected property name or '}' at line 1 column 2 | Single quotes (') are not allowed in JSON |
| Unquoted key | {name: "Asha"} | Expected property name or '}' in JSON at position 1 (line 1 column 2) | expected property name or '}' at line 1 column 2 | Expected '}' |
| Missing comma | {"a": 1 "b": 2} | Expected ',' or '}' after property value in JSON at position 8 (line 1 column 9) | expected ',' or '}' after property value in object at line 1 column 9 | Expected '}' |
| Missing closing brace | {"a": {"b": 1} | Expected ',' or '}' after property value in JSON at position 14 (line 1 column 15) | end of data after property value in object at line 1 column 15 | Expected '}' |
| Unescaped quote | {"quote": "She said "hi""} | Expected ',' or '}' after property value in JSON at position 21 (line 1 column 22) | expected ',' or '}' after property value in object at line 1 column 22 | Expected '}' |
| Comment | {"a": 1 // note} | Expected ',' or '}' after property value in JSON at position 8 (line 1 column 9) | expected ',' or '}' after property value in object at line 1 column 9 | Unrecognized token '/' |
undefined | {"a": undefined} | Unexpected token 'u', "{"a": undefined}" is not valid JSON | unexpected character at line 1 column 7 | Unexpected identifier "undefined" |
| Line break inside a string | a real newline between line1 and line2 | Bad control character in string literal in JSON at position 12 (line 1 column 13) | bad control character in string literal at line 1 column 13 | Unterminated string |
| Leading zero | {"pin": 004102} | Unexpected number in JSON at position 9 (line 1 column 10) | expected ',' or '}' after property value in object at line 1 column 10 | Expected '}' |
NaN | {"x": NaN} | Unexpected token 'N', "{"x": NaN}" is not valid JSON | unexpected character at line 1 column 7 | Unexpected identifier "NaN" |
| Empty input | (nothing) | Unexpected end of JSON input | unexpected end of data at line 1 column 1 | Unexpected EOF |
Firefox's messages all start with JSON.parse: and end with of the JSON data; that wrapper is trimmed above. Safari's all start with JSON Parse error:.
Reading the position
Chrome reports two numbers. position 27 counts characters from zero; line 1 column 28 counts from one, and that is the pair your editor shows. Firefox gives only the line and column. Safari gives neither, and five different mistakes in the table produce the same Expected '}', so on a long payload Safari's message alone won't find the problem. Paste the JSON into the JSON Formatter in Chrome or Firefox: it shows the browser's own message, so you get the column.
The column is where the parser noticed something was wrong, which is often one step after the actual mistake:
- Trailing comma: column 28 is the closing
}. The comma before it was fine until the parser found no key after it. Delete the comma, not the brace. - Unescaped quote: column 22 is the
hofhi. The quote before it closed the string early, so the parser expected a comma next. Write\"hi\"instead. - Missing closing brace: column 15 is one past the end of the text. The parser ran out of input while still inside the outer object. On a large file, count braces around the last object you edited rather than looking at the end.
Fixing each kind of mistake
- Quotes. Keys and strings take double quotes, always. Safari is the only engine that names single quotes; Chrome and Firefox just say they expected a property name.
- Comments. JSON has none. If a config file needs them, it is really JSONC or JSON5, and the program reading it must use a parser for that format.
- JavaScript-only values.
undefined,NaNandInfinityare valid JavaScript but not JSON.JSON.stringifyturnsNaNintonulland drops keys whose value isundefined, so hand-written JSON is usually where these come from. Usenull, or a string if the value must survive. - Line breaks in strings. A real newline inside quotes is a control character. Write
\ninstead. This one appears when text is pasted into a JSON file from a document. - Leading zeros. A JSON number can't start with 0 unless it is 0 itself or a decimal like 0.5. Codes such as PIN codes and account numbers belong in quotes,
"004102", which also stops them being treated as quantities later. - Empty input.
Unexpected end of JSON inputusually doesn't mean broken JSON at all: the text was empty. In code it most often comes from callingresponse.json()on an HTTP response with no body, such as a 204 No Content. Checkresponse.status, or readresponse.text()first and parse only if it isn't empty.
Once it parses
Valid JSON can still surprise you when a formatter rewrites it, because formatting parses and re-serializes the data. The JSON Formatter page lists the cases where the output differs from what you pasted, such as very long integers. If the JSON is headed for a spreadsheet next, the JSON to Excel, CSV, XML and YAML guide covers what changes in each format.
Related guides
- How to Format and Read Messy SQL Queries · A one-line report query, formatted step by step, and the AND/OR precedence bug the formatting exposes, plus why picking the right SQL dialect matters and a LEFT JOIN rule worth memorising.
- HEX, RGB and HSL: How the Conversions Work and Where They Round · #4F46E5 converted to RGB and HSL step by step, why 89.4% of all colours change when copied as whole-number HSL, why HSL lightness is not brightness (yellow and blue at 50% differ eightfold in contrast), and the alpha that hex can't write exactly.
- On-Page SEO Basics: Meta Tags, Robots.txt, and Favicons · The technical basics that decide how a page appears in search and link previews: title, description and canonical tags, what robots.txt can and can't do, and the favicon sizes search engines actually use.