How to Fix the JSON Trailing Comma Error
JSON does not allow trailing commas. Remove the comma before ] or }. This is the most common JSON syntax error. Unlike JavaScript, Python, and other languages that allow trailing commas, the JSON specification (RFC 8259) strictly forbids them.
Before and After
// INVALID — trailing comma after "b": 2
{
"a": 1,
"b": 2, <-- remove this comma
}
// VALID
{
"a": 1,
"b": 2
}
// INVALID — trailing comma in array
[1, 2, 3,] <-- remove this comma
// VALID
[1, 2, 3]
How to Catch Trailing Commas
- VS Code: Built-in JSON validation highlights trailing commas in red
- ESLint:
no-trailing-commasrule for JSON files - Command line:
python -m json.tool < file.json - Online: Use our JSON Validator
Regex to Remove Trailing Commas
// JavaScript — quick fix (use with caution)
jsonStr = jsonStr.replace(/,\s*([}\]])/g, '$1');
Warning: this regex can break JSON strings that contain ,} or ,] as literal text. Always validate after applying.
Try It Yourself
Use our JSON Validator to find and fix errors, or our JSON Formatter to clean up your JSON.
Frequently Asked Questions
Which formats allow trailing commas?
JavaScript (ES5+), JSON5, JSONC, Python, Rust, and Go all allow trailing commas. Standard JSON does not.
Why doesn't JSON allow trailing commas?
Douglas Crockford, who formalized JSON, chose simplicity: the grammar is smaller and parsers are simpler without optional trailing commas. This also avoids ambiguity with empty trailing elements.