You write what looks like perfectly reasonable JSON — every key quoted, every value valid — and JSON.parse() still throws a SyntaxError. The culprit is almost always a trailing comma: that one extra , sitting after the last item in an object or array. Here's why JSON forbids it, where it sneaks in, and how to get rid of it.
What Is a Trailing Comma?
A trailing comma (also called a "dangling comma" or "final comma") is a comma that appears after the last item in a list — with nothing following it before the closing bracket.
// Trailing comma in an object
{
"name": "Ada",
"score": 98, // ← this comma has no next item
}
// Trailing comma in an array
[1, 2, 3,]
// ^ same problem Both examples above will throw a SyntaxError when passed to JSON.parse().
Why Does JSON Forbid Trailing Commas?
The JSON grammar (defined in RFC 8259 ) specifies that a comma is a separator between elements — it must always have a value on both sides. The formal rule for a JSON object looks like this:
object = "{" [ member *( "," member ) ] "}"
member = string ":" value A comma must be followed by another member. If there's nothing after the comma, the grammar is invalid. This was a deliberate design choice: JSON prioritises simplicity and unambiguous parsing over developer convenience.
JavaScript took a different road. ES5 (2009) explicitly made trailing commas legal in array literals, and ES2017 extended this to function parameters. Trailing commas in JS are now common style — many linters even enforce them because they produce cleaner git diffs when you add a new item to a list.
The gap between what JavaScript allows and what JSON allows is the root of almost every trailing-comma error you'll ever see.
Where Trailing Commas Come From
Hand-edited config files
Config files like tsconfig.json, package.json, or app settings are edited by humans. After adding or removing an entry, it's easy to leave a stray comma behind. Many editors (VS Code, WebStorm) quietly accept JSONC (JSON-with-Comments) which allows trailing commas, so the file looks fine in the editor but breaks at runtime.
// You deleted "debug": true but forgot to remove the comma
{
"compilerOptions": {
"strict": true,
"target": "ES2020", // ← now trailing
}
}JavaScript → JSON serialisation
Developers sometimes build a JSON string manually by string-interpolating JavaScript values, or by using Array.join(','). If the logic adds a comma after every item unconditionally, the last item gets a trailing comma.
// ❌ naive manual serialisation
const parts = items.map(item => `"${item.name}": ${item.value}`);
const json = '{' + parts.join(',') + ',}'; // extra comma before }
// ✅ use JSON.stringify — it always produces valid JSON
const json = JSON.stringify(Object.fromEntries(items.map(i => [i.name, i.value])));LLM and AI output
Language models asked to "output JSON" frequently produce trailing commas. The model has seen enormous amounts of JavaScript code during training where trailing commas are legal, so it replicates the pattern. Always validate or repair AI-generated JSON before parsing it.
Copy-paste from JavaScript source
Copying an object literal out of a .js or .ts file and pasting it into a JSON context is one of the most common ways trailing commas appear. The source code was valid JavaScript; the destination only accepts JSON.
How to Find the Trailing Comma
The error message usually points you close to the problem, but not always exactly at the comma itself — the parser only notices something is wrong when it hits the unexpected } or ]:
SyntaxError: Expected double-quoted property name in JSON at position 58
// or
SyntaxError: Unexpected token '}', ..."score": 98,}" is not valid JSONWork backwards from the reported position. The trailing comma is the character immediately before the closing bracket that triggered the error.
For large JSON files, a regex search helps:
// Find trailing commas in objects/arrays
,\s*[}\]]How to Fix It
Option 1 — Delete it manually
Find the last item in the offending object or array and remove the comma after it. For small files this is the fastest approach.
Option 2 — Use a regex replacement (careful)
const fixed = raw.replace(/,\s*([}\]])/g, '$1');
JSON.parse(fixed); This works for the common case, but it's fragile: it can corrupt strings that contain ,} or ,] as literal characters. Use it only when you know the input doesn't contain such strings.
Option 3 — Use a proper repair parser
A repair parser understands the full JSON grammar and removes trailing commas only in the structural positions where they're invalid, without touching string content. This is the safest approach for production code or untrusted input.
Option 4 — Switch to JSONC for config files
If the trailing commas are in a config file you control, consider switching to a parser that supports JSONC (JSON with Comments). TypeScript's tsconfig.json already uses JSONC. For Node.js config files, libraries like @humanwhocodes/momoa or json5 parse a superset of JSON that allows trailing commas and comments.
When Trailing Commas Are Actually Allowed: JSONC and JSON5
Two JSON-adjacent formats deliberately allow trailing commas (and some other relaxations), and they're worth recognising:
- JSONC (JSON with Comments) — used by
tsconfig.json, VS Codesettings.json, and many other developer configs. JSONC allows////* */comments and trailing commas. The file is still labelled.json, but standardJSON.parse()rejects it — VS Code and tooling ship JSONC-aware parsers. - JSON5 — a fuller superset: comments, trailing commas, unquoted keys, single quotes, multi-line strings, hex numbers. Opt in with the
json5npm package orpyjson5in Python. Use it where humans hand-edit data and you control the parser; don't ship it over an API to clients that expect strict JSON.
Rule of thumb: keep trailing commas out of anything that crosses a strict JSON parser (API responses, request bodies, files consumed by random libraries), and embrace them inside JSONC/JSON5 only where the consumer is aware.
Preventing Trailing Commas
- Never build JSON strings manually. Use
JSON.stringify()— it always produces spec-compliant output. - Add a linter rule. ESLint's
jsonc/no-trailing-commasrule catches trailing commas in.jsonfiles on save. - Validate AI output. Always run
JSON.parse()(or a repair parser) on anything a language model generates before using it. - Use Prettier. Prettier reformats JSON files and removes trailing commas automatically on save.
Frequently Asked Questions
Does JSON allow trailing commas?
No. The JSON grammar in RFC 8259 defines a comma strictly as a separator between two values, so a comma with nothing after it before the closing } or ] is invalid. Every standard parser — JSON.parse(), Python's json module, Go's encoding/json — rejects it.
How do I remove a trailing comma from JSON?
For one file, delete the comma after the last item manually. For repeated or programmatic fixes, use JSON.stringify() to regenerate the JSON, a repair parser, or a tool like JSON Fix that strips structural trailing commas without touching string content.
Why does JavaScript allow trailing commas but JSON doesn't?
JavaScript added trailing commas in ES5 (arrays) and ES2017 (function parameters) as a developer-convenience feature that produces cleaner git diffs. JSON is a deliberately minimal interchange format that prioritises unambiguous parsing over convenience, so it never adopted them. See JSON vs JavaScript objects for the full list of differences.
What error does a trailing comma cause?
Usually SyntaxError: Unexpected token '}' or Expected double-quoted property name in JSON, because the parser expects another value after the comma and instead finds the closing bracket. This is one variant of the broader JSON.parse "Unexpected token" errors.
Fix It Now — No Setup Required
If you have a JSON string with trailing commas that you need fixed immediately, JSON Fix removes them automatically along with any other common issues — single quotes, unquoted keys, Python literals, JavaScript comments. Paste your JSON, click Repair & Format, and copy clean JSON back. No account, no upload, no data leaves your browser.
- JSON Fix — repair trailing commas and other common JSON errors
- Fix "[object Object] is not valid JSON" and other syntax errors — the complete guide to JSON syntax errors
- Fix Trailing Commas in JSON — quick reference guide with broken and fixed examples
- JSON Diff — compare the original and repaired JSON to make sure the fix is correct