JSON Trailing Comma Error: How to Find and Fix It
A trailing comma in JSON causes an immediate parse failure. The fix is always the same — remove the comma — but the error message varies by runtime, and the comma can hide in objects, arrays, or both.
What the error looks like
Different runtimes report the same problem differently:
// Node.js / V8
SyntaxError: Unexpected token '}' in JSON at position 42
// Firefox
SyntaxError: JSON.parse: unexpected character at line 4 column 1
// Safari
SyntaxError: JSON Parse error: Unexpected comma at end of array expression
// Python json module
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotesAll of them point to the character after the trailing comma, not the comma itself. The actual fix is one character before that position.
Trailing comma in an object
// ❌ Invalid — trailing comma after last property
{
"name": "Alice",
"role": "admin",
}
// ✅ Valid
{
"name": "Alice",
"role": "admin"
}Trailing comma in an array
// ❌ Invalid — trailing comma after last element
["read", "write", "delete",]
// ✅ Valid
["read", "write", "delete"]Nested trailing commas
Trailing commas can appear at any nesting level. The parser stops at the first one it finds.
// ❌ Invalid — trailing comma deep in a nested object
{
"user": {
"name": "Alice",
"permissions": ["read", "write",]
},
}
// ✅ Valid
{
"user": {
"name": "Alice",
"permissions": ["read", "write"]
}
}Why JSON forbids trailing commas
The JSON specification (RFC 8259) was intentionally minimal. Trailing commas were excluded to keep parsers simple and to avoid ambiguity about whether a trailing comma implies a missing value. JavaScript object literals added trailing comma support later as a developer convenience, which is why the two formats look similar but behave differently.
If you need a JSON-like format that supports trailing commas and comments, consider JSONC (used by VS Code settings) or JSON5. For strict data interchange, use JSON.
How trailing commas get introduced
- Hand-editing a JSON file and removing the last entry without removing its comma
- Copy-pasting a JavaScript object literal into a JSON context
- Building JSON by string concatenation instead of
JSON.stringify() - A template or code generator that adds commas after every item
Prevent trailing commas in generated JSON
Always use JSON.stringify() to produce JSON programmatically. It never introduces trailing commas.
// Safe: JSON.stringify handles commas correctly
const payload = JSON.stringify({ name: "Alice", role: "admin" })
// → '{"name":"Alice","role":"admin"}'
// Unsafe: manual concatenation is error-prone
const unsafe = '{"name":"' + name + '","role":"' + role + '",}'For more on safe JSON generation, see the JSON.stringify tutorial.
Fix pasted JSON with a validator
When the JSON comes from an external source — an API response, a config file, or a colleague's payload — paste it into a validator first. It will highlight the exact comma position so you can fix it in seconds rather than scanning the file manually.
Find the trailing comma instantly
Paste your JSON into Jsonic's JSON Validator. It shows the exact line and column of every syntax error, including trailing commas.
Open JSON Validator