How to Fix Invalid JSON: 10 Common Errors and Their Fixes
Short answer
Paste the JSON into a validator to find the exact line and character of the error, then check the usual suspects: trailing commas, single quotes, unquoted keys, comments, missing commas or brackets, and unescaped characters in strings. JSON is stricter than JavaScript: keys and strings need double quotes, and comments and trailing commas are not allowed.
A single misplaced character makes an entire JSON document invalid, and the resulting error messages, such as “Unexpected token } in JSON at position 87” or “Expecting property name enclosed in double quotes”, are not always obvious. The good news is that almost every JSON error falls into a handful of patterns. Once you know them, you can fix most problems in seconds.
Step 1: Find the exact location
Paste your JSON into the JSON formatter and validator. It shows the line of the first syntax error and, once the JSON is valid, pretty-prints it so the structure is easy to read. Error positions point to where the parser gave up, which is often just after the real mistake, so look at the line above too. Fix one error at a time and validate again, because one mistake can hide others.
Step 2: Check the 10 most common errors
1. Trailing commas
Invalid: {"a": 1, "b": 2,} and [1, 2, 3,]
Valid: {"a": 1, "b": 2}
JavaScript allows a comma after the last item; JSON does not.
2. Single quotes
Invalid: {'name': 'Ana'}
Valid: {"name": "Ana"}
JSON strings and keys must use double quotes. This often happens when a Python dictionary is printed instead of being converted with json.dumps().
3. Unquoted keys
Invalid: {name: "Ana"}
Valid: {"name": "Ana"}
4. Comments
Invalid: {"debug": true // turn off later}
Standard JSON has no comments. Remove them, or move notes into a field such as "_comment". Some tools accept “JSON with comments” (for example, certain editor settings files), but a strict parser will reject it.
5. Missing commas
Invalid: {"a": 1 "b": 2}
Valid: {"a": 1, "b": 2}
Very common when lines are copied and pasted together.
6. Mismatched or missing brackets
Every { needs a } and every [ needs a ], in the right order. Formatting the JSON with indentation makes unbalanced brackets obvious. An error at the very end, such as “Unexpected end of JSON input”, usually means a closing bracket is missing or the data was cut off.
7. Unescaped characters inside strings
Inside a string, a double quote must be written as ", a backslash as \, and a line break as
. Windows paths are a classic trap: "C:
ewile" is invalid or wrong; write "C:\new\file". Literal tab and newline characters inside strings are also not allowed.
8. Invalid values
JSON only allows strings, numbers, objects, arrays, true, false and null, all lowercase. These are invalid: True, None, undefined, NaN, Infinity and functions.
9. Invalid numbers
Numbers cannot have leading zeros (007), a leading plus sign (+5), a trailing decimal point (5.), or hexadecimal notation (0xFF). Values like ZIP codes or phone numbers that need leading zeros should be strings: "zip": "02134".
10. Invisible characters and wrong encoding
Copying from word processors or chat apps can introduce “smart” curly quotes (“ ”) instead of straight quotes ("), or non-breaking spaces. A byte order mark at the start of a file can also break some parsers. JSON exchanged between systems should be UTF-8. If a file looks correct but will not parse, retype the quotes or check the encoding.
Step 3: Watch out for these situations
- Getting HTML instead of JSON: “Unexpected token < in JSON at position 0” almost always means the server returned an HTML error page (which starts with <) instead of JSON. Check the URL, the status code and the response body.
- Empty responses: “Unexpected end of JSON input” can mean the response was empty.
- Double-encoded JSON: a string like
"{"a":1}"is JSON containing a string of JSON. Parse it twice, or fix the code that encoded it twice. - Building JSON by hand: concatenating strings to make JSON is error-prone. Use your language’s serializer, such as
JSON.stringify()in JavaScript orjson.dumps()in Python.
Quick reference
| Error message (varies by tool) | Likely cause |
|---|---|
| Unexpected token } or ] | Trailing comma before the bracket |
| Unexpected token ' | Single quotes |
| Expecting property name | Unquoted key or trailing comma in an object |
| Unexpected token < at position 0 | HTML page instead of JSON |
| Unexpected end of JSON input | Missing closing bracket or empty response |
| Bad control character in string | Literal newline or tab inside a string |
After fixing: format, compare and convert
Once your JSON is valid, the formatter can beautify it with 2 or 4 spaces, sort keys or minify it for production. To see what changed between a broken and a fixed version, paste both into the text diff checker. If you need the data in a spreadsheet, the JSON to CSV converter turns an array of objects into rows, and the code beautifier tidies JavaScript, HTML and CSS.
New to JSON? Start with JSON basics for beginners. If your JSON contains encoded data, see what Base64 encoding is, and for ID fields read what a UUID is.
Frequently asked questions
How do I find where my JSON is invalid?
Paste it into a JSON validator. It reports the line of the first error. Check that line and the one before it, fix the problem and validate again.
Are trailing commas allowed in JSON?
No. A comma after the last item in an object or array makes the JSON invalid, even though JavaScript allows it.
Can JSON have comments?
Not in standard JSON. Remove comments or put notes in a regular field. Some tools accept JSON with comments, but strict parsers do not.
Why do I get an “Unexpected token” error at position 0?
If the unexpected character is an opening angle bracket, the response is HTML, usually an error or login page, rather than JSON. Check the request URL and the server response.
Does JSON require double quotes?
Yes. Keys and string values must be wrapped in double quotes. Single quotes are not valid JSON.