How to Fix Common JSON Syntax Errors
JSON looks forgiving, yet a single misplaced character can make an entire document unreadable. If you have ever seen "Unexpected token" in a console, you know the frustration. The good news is that nearly all JSON failures fall into a short list of patterns. Once you recognise them, you can fix most problems in seconds.
Why JSON Is So Strict
JSON was designed to be trivial to parse in every programming language, so its grammar is deliberately small and has no tolerance for shortcuts. Unlike a JavaScript object literal, it permits no comments, no trailing commas, no single quotes and no unquoted keys. A parser reads from left to right and stops at the first character that breaks the rules, which is why error messages usually point at the place where the parser gave up rather than at the true cause.
1. Trailing Commas
The most common mistake is a comma after the last item in an object or array:
{ "name": "Ada", "age": 36, }
Programmers coming from JavaScript, Python or Go are used to trailing commas being allowed. In JSON they are an error. Remove the comma after the final element and the document becomes valid. This often appears when someone deletes the last line of a list and forgets the comma on the line above.
2. Single Quotes Instead of Double Quotes
JSON strings and property names must use double quotes. {'name': 'Ada'} is rejected by every compliant parser. Replace the single quotes with double quotes, and escape any double quote inside a string with a backslash. If a value contains many apostrophes, such as in English text, make sure you only change the quotes that wrap the string, not the apostrophes inside it.
3. Unquoted Keys
Writing { name: "Ada" } is valid JavaScript but invalid JSON. Every key has to be a quoted string. This error is easy to introduce when copying a console log or a configuration object into a JSON file.
4. Comments
JSON does not support // or /* */ comments. Configuration files that appear to allow them, such as some editor settings, actually use extended dialects like JSONC. If you must annotate a strict document, add a dedicated field such as "_comment" or keep documentation in a separate file.
5. Missing or Extra Commas and Brackets
Forgetting a comma between two properties produces an error at the start of the next key. Missing closing braces or brackets usually produce "Unexpected end of input", because the parser reached the end of the text while still expecting more. Count your opening and closing characters, and use consistent indentation: a formatter that shows nesting makes an unbalanced bracket visible immediately.
6. Invalid Values
JSON allows only strings, numbers, true, false, null, objects and arrays. Values such as undefined, NaN, Infinity, dates written as bare objects, and numbers with leading zeros like 007 are all invalid. Convert them to strings or null as appropriate. Python users should remember that True, False and None must be written in lowercase JSON form.
7. Unescaped Special Characters
Newlines, tabs and backslashes inside strings must be escaped as \n, \t and \\. Windows file paths are a classic trap: "C:\Users" contains an invalid escape, and must be written as "C:\\Users".
A Reliable Debugging Workflow
Start by pasting the document into a validator that reports the line and column of the first error, such as our JSON Formatter. Fix that one problem, then validate again, because later errors are hidden until earlier ones are resolved. Look slightly before the reported position as well, since the real mistake, like a missing quote, often sits just before the parser noticed it. For API responses, check that you are not parsing an HTML error page or a response that was truncated by a timeout.
Preventing Errors
Generate JSON with a library function such as JSON.stringify instead of building strings by hand, validate files in continuous integration, and configure your editor to flag syntax problems as you type. These habits remove nearly every error described above before it reaches production.