Fix JSON Error: Trailing Comma
A trailing comma after the last element in an object or array is the most common reason JSON fails to parse. Here's why it happens and how to fix it.
The Error Message
Trailing commas produce different error messages depending on the parser, but they all point to the same problem:
SyntaxError: Expected double-quoted property name in JSON at position 48
SyntaxError: Unexpected token ']' at position 27
JSON.parse: expected property name or '}' at line 4 column 1Why JSON Doesn't Allow Trailing Commas
The JSON specification (RFC 8259) defines a strict grammar where a comma is a separator between values, not a terminator after each value. That means a comma must always appear between two elements — never after the last one.
JavaScript, Python, and many other languages allow trailing commas as a convenience. But JSON is a data interchange format, not a programming language. Its strictness is intentional — it reduces ambiguity and keeps parsers simple.
Trailing Comma in an Object
The extra comma after the last key-value pair makes the parser expect another property name, and it gets a closing brace instead.
// ❌ Broken — trailing comma after "active"
{
"name": "Alice",
"age": 30,
"active": true,
}// ✅ Fixed
{
"name": "Alice",
"age": 30,
"active": true
}Trailing Comma in an Array
Same problem in arrays — the parser expects another value after the comma and instead finds the closing bracket.
// ❌ Broken — trailing comma after 3
{
"ids": [1, 2, 3,]
}// ✅ Fixed
{
"ids": [1, 2, 3]
}Nested Trailing Commas
In deeply nested structures, trailing commas can hide at multiple levels. Always check inner objects and arrays, not just the top level.
// ❌ Broken — trailing commas at two levels
{
"users": [
{
"name": "Alice",
"roles": ["admin", "editor",],
},
]
}// ✅ Fixed
{
"users": [
{
"name": "Alice",
"roles": ["admin", "editor"]
}
]
}How to Find Trailing Commas
- Use the error position. The character index in the error message usually points right at the closing bracket or brace. Look at the line just above it for the stray comma.
- Search with a regex. The pattern
,\s*[\]\}]matches a comma followed by optional whitespace and then a closing bracket or brace. Most editors support regex search. - Use an online validator. Paste your JSON into a validator that highlights the exact error location.
How to Fix It Step by Step
- Open the JSON in an editor with syntax highlighting (VS Code, for example, underlines JSON errors in red).
- Jump to the line referenced by the error. Look for a comma immediately before a
}or]. - Delete the trailing comma.
- Validate the entire document again — there may be more than one.
Prevention Tips
- Use
JSON.stringify()or your language's JSON serializer to generate JSON. They never produce trailing commas. - Configure your editor's JSON formatter to remove trailing commas on save.
- If you prefer trailing commas in code, use JSONC (JSON with Comments) for config files — VS Code supports it natively for
tsconfig.jsonand similar files. - Add a JSON lint step to your CI pipeline to catch these before they hit production.
Fix it automatically: Paste your broken JSON into our JSON Repair tool — it handles this error and dozens more. Or validate your JSON first with our JSON Validator.