Trailing Comma in JSON: Why It's Invalid and How to Fix It
A comma in JSON serves as a separator between values, not a terminator after each one. Placing a comma after the final item in an object or array — directly before the closing } or ] — violates the grammar defined in RFC 8259, the specification that governs JSON.
This is the single most common JSON syntax error because JavaScript's visually identical object and array literal syntax does allow trailing commas (since ES5). Developers frequently copy a JS object into a .json file or paste it into an API request body without realizing the two formats follow different grammars — and every JSON parser in existence will reject it.
{"name": "Alice", "age": 30,}{"name": "Alice", "age": 30}[1, 2, 3,]
[1, 2, 3]
1What error message will I see?
Every JSON parser rejects trailing commas, but the wording of the error message varies dramatically across environments. None of them actually say "trailing comma" — they describe what the parser expected to find instead, which is why this error is often confusing to debug at first.
| Environment | Error message |
|---|---|
| Node.js / V8 (Chrome) | SyntaxError: Unexpected token } in JSON at position N |
| Python (json module) | json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes |
| Firefox | SyntaxError: JSON.parse: unexpected character |
| Safari | SyntaxError: JSON Parse error: Unexpected comma at end of array expression |
Safari is the only environment that directly mentions a comma. In Node/Chrome, you'll see Unexpected token } or Unexpected token ] because the parser hits the closing bracket where it expected another value. Python's json.loads() reports it as expecting a property name — because after a comma, the parser assumes another key-value pair must follow.
2Why does JavaScript allow this but JSON doesn't?
JSON was designed as a strict, minimal data-interchange format intended to be parsed identically by every language on every platform. RFC 8259 intentionally kept the grammar as small and unambiguous as possible — no comments, no trailing commas, no unquoted keys — because each extra tolerance creates room for parser disagreements across implementations.
JavaScript's object and array literal syntax, by contrast, is part of a programming language that's maintained and evolved by TC39. Trailing commas were formally permitted in ES5 (2009) as an editing convenience: they make it easier to reorder lines and produce cleaner Git diffs. That's a reasonable trade-off for hand-edited source code, but it's not safe for a data format that has to be parsed by Python, Go, Java, Rust, C#, and every other language identically.
The practical result: const obj = {a: 1, b: 2,} is perfectly valid JavaScript, but the string '{"a": 1, "b": 2,}' will fail if you pass it to JSON.parse() — even in the same JavaScript engine.
3How to find and fix it
For existing JSON you need to debug: paste it into our JSON Formatter tool. The validator will point to the exact line and character position where parsing failed, so you can jump straight to the trailing comma and remove it.
For JSON you're generating programmatically: never build JSON strings via manual string concatenation or template literals — this is the #1 source of trailing commas in production code (e.g. looping over items and appending + "," after each one, then forgetting to trim the last one). Always use your language's built-in serializer:
# Python
items = ["a", "b", "c"]
json_str = "[" + ",".join(
f'"{i}"' for i in items
) + ",]" # trailing comma!# Python import json items = ["a", "b", "c"] json_str = json.dumps(items) # '["a", "b", "c"]' — always valid
// JavaScript
const body = `{
"user": "${name}",
"role": "${role}",
}`; // trailing comma!// JavaScript
const body = JSON.stringify({
user: name,
role: role,
}); // always valid JSON output4What if I actually want trailing commas?
Two well-known JSON supersets do permit trailing commas (and comments):
- JSON5 — a formal extension of JSON that adds trailing commas, single-quoted strings, comments, and more. Used in some build tools and config files.
- JSONC (JSON with Comments) — the format VS Code uses for
settings.json,tsconfig.json, and other editor configs. Allows trailing commas and//comments.
JSON.parse(), Python's json.loads(), and any strict JSON parser. They are designed for configuration files consumed by tooling that explicitly supports them — not for API request/response bodies, data interchange, or any context where you need standard JSON compliance.Paste your JSON and find the trailing comma instantly.
Our formatter highlights the exact line and position where the syntax breaks — fix it in seconds, right in your browser.