Fix: Trailing Comma in JSON
Trailing commas are perfectly legal in modern JavaScript — but they are strictly forbidden in JSON. Even a single extra comma will cause every standard JSON parser to throw a syntax error.
What Is a Trailing Comma?
A trailing comma is a comma that appears after the last item in an array or after the last key-value pair in an object. It "trails" the last element rather than separating two elements.
Invalid JSON — trailing comma in object
{
"name": "Alice",
"role": "admin", ← trailing comma
}Valid JSON
{
"name": "Alice",
"role": "admin"
}Invalid JSON — trailing comma in array
["apple", "banana", "cherry",]Valid JSON
["apple", "banana", "cherry"]Why JSON Does Not Allow Trailing Commas
The JSON grammar defined in RFC 8259 specifies that a comma always separates two values. After the last value there is nothing to separate, so a comma is a grammar violation. This is intentional: JSON is a minimal format designed to be implemented identically in every language without ambiguity.
JavaScript ES5 added trailing comma support to object and array literals as a developer convenience, which is why developers frequently copy JS object syntax into JSON files and hit this error.
How to Fix It
Option 1: Use the formatter below
Paste your JSON, click Format, and download or copy the clean output.
Option 2: Manual regex (quick hack)
In VS Code or any editor that supports regex find-and-replace, use this pattern:
Find: ,\s*([}\]])
Replace: $1This strips trailing commas before closing braces and brackets.
Option 3: JSON5 parser (if you can change the toolchain)
If you are working with config files you control and need trailing commas, switch to JSON5:
import JSON5 from "json5";
const config = JSON5.parse(`{
"name": "Alice",
"role": "admin", // trailing comma + comment — both OK in JSON5
}`);Fix Your JSON Here
Frequently Asked Questions
Why are trailing commas invalid in JSON but valid in JavaScript?▾
JavaScript object and array literals have allowed trailing commas since ES5 (2009) as a convenience for developers. The JSON specification (RFC 8259) was designed to be a minimal, language-independent data format, so it deliberately excludes this feature to keep the grammar unambiguous.
What error do I get from a trailing comma in JSON?▾
In JavaScript you will see: SyntaxError: Unexpected token } in JSON. In Python: json.JSONDecodeError: Expecting property name enclosed in double quotes. In Go: invalid character '}' looking for beginning of object key string.
How do I fix trailing commas in JSON automatically?▾
Paste the JSON into the formatter on this page and click Format — it will parse the JSON and re-emit it without trailing commas. Alternatively, a regex like /,\s*([}\]])/ → '$1' will strip most trailing commas, though a proper parser is safer.
Does JSON5 allow trailing commas?▾
Yes. JSON5 is a superset of JSON that adds trailing commas, single-quoted strings, comments, and more. However, JSON5 is not standard JSON and requires a dedicated parser (json5 npm package). Standard JSON parsers will still reject it.
How can I prevent trailing commas when generating JSON programmatically?▾
Never build JSON by concatenating strings. Use JSON.stringify() in JavaScript, json.dumps() in Python, or the equivalent in your language. These serialisers always produce valid JSON without trailing commas.
More JSON Error Guides
If jsondecode.com saved you time, share it with your team
Free forever. No ads. No sign-up. Help other developers find it.