"Unexpected token", "Expected property name", "Unexpected end of JSON input". Parser error messages are famously unhelpful, but almost every invalid JSON file fails for one of eight reasons. Work through this list and you will find the problem.
First: find the line
Reading a whole file hunting for a stray comma is slow. Use a tool that tells you where parsing failed. Docento's Text & Markdown Editor checks .json files as you type and shows a bar such as "JSON: Line 4: Unexpected character" that you can click to jump to the line. Remember that the reported line is where the parser noticed the problem, which can be just after the real mistake.
1. Trailing comma
{ "a": 1, "b": 2, }
JSON does not allow a comma after the last item in an object or array. Delete it. This is the most common error, particularly after deleting the last line of a list.
2. Missing comma
{ "a": 1 "b": 2 }
Items must be separated by commas. The parser usually complains at the start of the second item. Add the missing comma.
3. Single quotes
{ 'name': 'Ada' }
JSON requires double quotes for strings and property names. Replace single quotes with double quotes. Mind apostrophes inside strings, which are fine in double-quoted text.
4. Unquoted property names
{ name: "Ada" }
JavaScript objects allow bare keys, JSON does not. Quote every key.
5. Comments
{ "a": 1 // the count }
Standard JSON has no comments. Remove them, or move the information into a field such as "_comment". If you want comments in a configuration file, consider a format that supports them; see JSON with comments.
6. Unescaped characters inside strings
Strings cannot contain a raw newline, a raw tab or an unescaped double quote. Use \n, \t and \". Windows file paths need doubled backslashes: "C:\\Users\\Ada". A single backslash followed by an unexpected character is invalid.
7. Invalid values
undefined,NaNandInfinityare not JSON.True,FalseandNone(Python spellings) are not JSON; usetrue,falseandnull.- Numbers cannot have leading zeros (
007), a leading plus, or a trailing decimal point (5.). - Hex numbers (
0xFF) are not allowed.
8. Mismatched brackets or truncated files
Every { needs a } and every [ needs a ]. A truncated download or a copy-paste that missed the last line ends in "Unexpected end of input". Check that the file ends where it should and that nesting matches. Many editors highlight the matching bracket when you click one.
Other causes worth checking
- Byte order mark or hidden characters at the start of the file, often from Windows editors. See text file encodings.
- Smart quotes introduced by a word processor, such as curly
“and”instead of". - Multiple JSON documents in one file, such as two objects back to back. That is JSON Lines or concatenated JSON, not a single valid document.
- Text before or after the JSON, such as a stray log line or a pasted label.
Fix it step by step
- Open the file in an editor with a syntax checker.
- Go to the reported line, and also look at the line above it.
- Fix one error at a time, since a parser stops at the first one.
- Use Format JSON to re-indent once valid, which makes remaining structure problems visible.
Prevent it
Generate JSON with a library rather than by assembling strings by hand. Libraries escape correctly and never emit trailing commas. When you must edit by hand, edit in a tool that validates as you type.
Takeaway
Trailing commas, missing commas, single quotes, bare keys, comments, unescaped characters, invalid values and mismatched brackets cover almost every invalid JSON file. Locate the line with a syntax checker, fix one error at a time and re-check.