Trailing Commas in JSON: Why Parsers Fail
Learn why trailing commas break strict JSON, how they sneak into configs and LLM output, and how to remove them safely.
What a trailing comma is
In JSON, commas separate elements in arrays and properties in objects. A trailing comma is a comma after the last element, immediately before ] or }. Example: {"a": 1, "b": 2,}. JavaScript object literals and many config formats accept that pattern. JSON does not.
Because the illegal character is often the last edit someone made—“I’ll add another field later”—this is one of the most common real-world syntax errors.
Where trailing commas come from
Humans introduce them when editing JSON in editors that borrow JS habits. Code generators and pretty printers aimed at TypeScript sometimes emit them. LLMs emit them constantly because training data includes TypeScript, Python, and YAML-adjacent examples where trailing commas are idiomatic.
Diffs and merge conflicts can also leave stray commas when a property is deleted but the previous separator remains.
How to fix them without breaking strings
A naive search-and-replace for /,\s*([}\]])/ can work on simple documents but is unsafe inside string values. Robust repair walks the token stream (or an error-tolerant parser) so commas inside strings stay untouched.
fixjson.dev removes trailing commas as part of its structural repair pass, then re-parses. If other issues remain—missing quotes, comments, Python literals—those transforms run in the same workshop session so you are not stuck fixing one class of error at a time.
Prevention in teams
Add a CI step that runs jq or JSON.parse on committed JSON files. Configure editors to use a JSON (not JSONC) language mode for files your production runtime will parse strictly. If you need comments or trailing commas in repo configs, store JSONC/JSON5 and compile to plain JSON during build.