JSON Trailing Comma Error: How to Find and Fix It
Last updated:
A trailing comma after the last value in a JSON object or array — {"a":1,} or [1,2,] — causes an immediate SyntaxError in every JSON parser, because RFC 8259 explicitly forbids trailing commas. The error message points to the closing bracket (} or ]), not the comma itself — V8 says {"SyntaxError: Unexpected token } in JSON at position N"}, Python says JSONDecodeError: Expecting value. JSON5 and JSONC (VS Code config files) both allow trailing commas, so the error only appears when the file is consumed by a standard JSON parser. This guide covers finding trailing commas by error position, automated fixers (json-repair npm package, jq, sed one-liners), and linting rules to prevent them.
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 ValidatorFrequently asked questions
Why does JSON not allow trailing commas?
RFC 8259 excluded trailing commas for simplicity and to avoid ambiguity. JavaScript object literals added them later as a convenience, creating the confusion between the two superficially similar formats.
How do I find and remove trailing commas in JSON?
Paste into a validator for the exact line. To fix, find commas followed only by whitespace before a closing } or ]. The jsonrepair npm package removes them automatically.
What is JSON5 and does it allow trailing commas?
JSON5 is a superset of JSON that explicitly allows trailing commas, single quotes, unquoted keys, and comments. Parse it with the json5 npm package. It is not suitable for API data interchange.
How do I fix trailing commas automatically?
Use jsonrepair: import {'{ jsonrepair }'} from 'jsonrepair'. In VS Code, Prettier with format-on-save removes trailing commas from JSON files automatically.
Does jq remove trailing commas?
No — jq uses a strict JSON parser and errors on trailing commas. Pre-process the file withjsonrepair before piping to jq.
Can VS Code prevent trailing commas in JSON?
Yes. Install Prettier and enable editor.formatOnSave for JSON files. Prettier removes trailing commas when formatting. The jsonc-eslint-parser plugin can also lint for trailing commas.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- JavaScript: The Definitive Guide (7th Edition) — David FlanaganThe complete reference for the language JSON came from — serialization, async, and the full standard library.
As an Amazon Associate, Jsonic earns from qualifying purchases.