Common JSON errors and how to fix them
Most invalid JSON fails for one of ten reasons. Here is what each one looks like, why it happens and the smallest change that fixes it.
4 min readUpdated MasterDev
Read the message first
Every parser tells you where it gave up, but each browser says it differently:
- Chrome and Edge:
Unexpected token '}', ..."age": 31,}" is not valid JSON, or in older versionsUnexpected token } in JSON at position 27. - Firefox:
JSON.parse: expected double-quoted property name at line 3 column 1 of the JSON data. - Safari:
JSON Parse error: Expected '}'.
Positions count characters from zero. The real mistake is usually just before the reported spot: the parser only notices something is wrong when the next character does not fit. Paste the document into the formatter and the status line shows the same message your code gets.
Trailing commas
{
"name": "Ana",
"roles": ["admin", "editor",],
}{
"name": "Ana",
"roles": ["admin", "editor"]
}JavaScript, Python, Go and Rust all allow a comma after the last item; JSON does not. It usually appears after someone deletes the last line of a hand-edited file, or when a template prints a comma after every item. Remove the comma before ] or }.
Single quotes
{'name': 'Ana', 'active': True, 'manager': None}{"name": "Ana", "active": true, "manager": null}Strings and keys must use double quotes. The example above is a classic: it is what Python prints when you call print(data) or str(data) on a dict, with True and None giving it away. Use json.dumps(data) instead.
Keys without quotes
{id: 1042, name: "Ana"}{"id": 1042, "name": "Ana"}This is a JavaScript object literal, not JSON. It happens when an object is copied from source code or from a browser console. In JavaScript, JSON.stringify(obj) gives you the JSON version.
Comments
{
// retry for slow networks
"timeoutMs": 5000
}{
"_comment": "retry for slow networks",
"timeoutMs": 5000
}JSON has no comments. Some files that look like JSON do accept them, such as tsconfig.json and VS Code settings, but those are JSONC, read by special parsers. For real JSON, delete the comment or move it into a field your code ignores.
NaN, Infinity and undefined
{"ratio": NaN, "limit": Infinity, "parent": undefined}{"ratio": null, "limit": null}JSON numbers must be finite, and undefined does not exist. Use null, a string such as "Infinity", or leave the key out. Watch out for Python: json.dumps(float('nan')) writes NaN by default, which other parsers reject. Pass allow_nan=False to catch it early. JavaScript's JSON.stringify quietly turns NaN into null and drops undefined keys.
Number formats
[007, +1, 0xFF, 1., .5][7, 1, 255, 1.0, 0.5]JSON numbers are decimal only: no leading zeros, no plus sign, no hex, and a digit is required on both sides of the decimal point. If a leading zero matters (postal codes, phone numbers, product codes), it is not a number: store it as a string, "007".
Line breaks and backslashes in strings
{"path": "C:\Users\ana", "note": "line one
line two"}{"path": "C:\\Users\\ana", "note": "line one\nline two"}A backslash starts an escape sequence, and only a few are allowed (\n, \t, \", \\, \uXXXX and a few more). \U and \a are not among them, so Windows paths need doubled backslashes. Raw line breaks and tabs inside a string must be written as \n and \t. This error almost always means the JSON was built by gluing strings together; the guide on escaping JSON shows how to let a serializer do it.
Missing commas, open brackets and empty input
[
{"sku": "KB-100"}
{"sku": "MS-220"}Two mistakes here: no comma between the objects, and no closing ]. The formatter reports the first one; fix it and validate again. Two messages deserve a special mention:
Unexpected end of JSON inputmeans the text stopped early: a truncated file, a response that was cut off, or an empty string.JSON.parse('')fails with exactly this message, so check that the response body is not empty.Unexpected token '<'means you received HTML, not JSON. The server probably answered with an error page, a login page or a 404. Log the response text before parsing it.
Invisible and look-alike characters
Some errors point at a spot that looks perfectly fine. The cause is usually a character you cannot see or that only looks like the right one:
- Byte order mark (U+FEFF) at the very start of a file saved by some Windows editors. Save as UTF-8 without BOM.
- Non-breaking spaces (U+00A0) copied from a web page or a document. JSON only allows ordinary spaces, tabs and line breaks between tokens.
- Curly quotes
“ ”added by word processors and chat apps. Replace them with straight".
Stop them from coming back
- Produce JSON with a serializer (
JSON.stringify,json.dumps, Jackson,encoding/json), never by concatenating strings. - Validate JSON files in CI:
jq empty config.jsonorpython -m json.tool config.jsonboth fail on invalid input. - Tell your editor the file type. VS Code underlines trailing commas and comments in
.jsonfiles.