Trailing Commas in JSON: Why They Break Parsing
A trailing comma is a comma after the last item in an array or object, like [1, 2, 3,]. It is perfectly legal in JavaScript, Python, and most programming languages, which is exactly why it trips so many people up in JSON: the JSON specification forbids it. This is one of the single most common reasons JSON fails to parse. Here is why the rule exists, how to find and fix a trailing comma fast, and how to stop writing them.
What a trailing comma looks like
The comma in question sits between the last element and the closing bracket or brace:
{
"name": "Ada",
"roles": ["admin", "dev"],
}
That comma after ["admin", "dev"] is a trailing comma, and it makes the whole document invalid JSON. The same applies inside arrays:
[
"red",
"green",
"blue",
]
Both examples parse fine as JavaScript literals. Both are rejected by JSON.parse, Python’s json.loads, and every spec-compliant JSON parser.
Why JSON forbids them
JSON was designed to be a strict, minimal data format, not a programming language. Douglas Crockford, who specified JSON, deliberately kept the grammar tight so that parsers would be simple and behavior would be identical everywhere. A comma in JSON means “another element follows.” A trailing comma promises an element that never arrives, so the grammar simply does not allow it.
That strictness is a feature. It is the reason a JSON file written by a Ruby service parses identically in a browser, a Go backend, and a Python script. The tradeoff is that conveniences you are used to in code, trailing commas and comments among them, are left out. If you want the format fundamentals, the guide on what JSON is covers the full grammar.
The errors you will see
Different parsers phrase it differently, but they all point at the comma or the bracket right after it.
- JavaScript:
Unexpected token ] in JSON at position 42orUnexpected token } in JSON - Python:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes(for an object) orExpecting value(for an array) - Many linters:
Trailing commadirectly
The JavaScript message is the giveaway: an Unexpected token ] or } almost always means a trailing comma sits just before that bracket. The position number points to the bracket, so look at the character immediately before it. For the broader family of these errors, see fixing Unexpected token errors.
How to fix it fast
For a single file, the fastest fix is to let a tool find it. Paste the JSON into the JSON parser and it points to the exact comma that breaks parsing, which is faster than scanning a large file by eye. Running the document through the JSON formatter does the same job and re-indents everything so the structure is easy to read while you are at it.
In code, do not hand-edit JSON strings to strip commas. Build a native object or array and serialize it, which can never produce a trailing comma:
const data = { name: "Ada", roles: ["admin", "dev"] };
const json = JSON.stringify(data); // always valid, no trailing comma
import json
data = {"name": "Ada", "roles": ["admin", "dev"]}
text = json.dumps(data) # always valid
Generating JSON with JSON.stringify or json.dumps instead of string concatenation eliminates the entire class of comma errors.
How to stop writing them
Prevention beats cleanup. A few habits make trailing commas a non-issue:
- Format on save. Wire a formatter or
prettierinto your editor so JSON is validated every time you save. A trailing comma shows up immediately, not three commits later. - Lint your config files. Tools like
jsonlintor a CI check catch trailing commas before a bad file merges. - Generate, do not type. Whenever JSON is produced programmatically, use the language’s serializer rather than building strings.
- Know where it hurts most. Config files edited by hand,
package.json,tsconfig.json, CI YAML that embeds JSON, are where trailing commas creep in, because humans add and remove lines there.
When you actually want trailing commas: JSON5 and JSONC
Trailing commas are genuinely convenient when you are editing config by hand, because you can reorder or append lines without fixing the comma on the previous line. Two relaxed supersets of JSON allow them:
- JSON5 allows trailing commas, comments, unquoted keys, and single quotes. It is meant for human-edited config.
- JSONC (JSON with Comments) is what editors like VS Code use for their settings files, and it allows comments and trailing commas.
The catch is that these are not JSON. A strict JSON.parse will still reject a JSON5 file. Use them only where the consumer explicitly supports them, such as a build tool’s config, and keep data that crosses systems as strict JSON. If you need to move data between formats entirely, the JSON to XML and YAML formatter tools cover those conversions.
Frequently asked questions
Does JSON allow trailing commas?
No. The JSON specification forbids a comma after the last element of an array or object. A trailing comma makes the document invalid and causes a parse error, even though the same syntax is legal in JavaScript and Python.
Why does my JSON fail with “Unexpected token ]”?
That almost always means a trailing comma sits just before the ] or }. The parser expected another value after the comma but found the closing bracket instead. Remove the comma, or run the file through the JSON parser to find it.
How do I remove trailing commas automatically?
Run the file through the JSON formatter, which rejects and locates them, or generate JSON with JSON.stringify / json.dumps instead of building strings by hand. A formatter or linter on save prevents them going forward.
Can I use trailing commas in a config file?
Only if the tool reading it supports JSON5 or JSONC. VS Code settings, for example, allow them. Strict JSON parsers do not, so never rely on trailing commas in data that crosses between systems.
For the rest of the toolkit, including validating, viewing, and converting your data, see the roundup of free JSON tools.
Leave a Reply