Fix JSON Trailing Comma Error: Quick Solutions
A trailing comma in JSON is one of the most common parse errors developers encounter - usually discovered at the worst moment, when deploying a config file or calling an API. Here is exactly what causes it, how to find it quickly, and how to fix it permanently.
What Is the JSON Trailing Comma Error?
A trailing comma is a comma that appears after the last element in a JSON object or array, immediately before the closing brace or bracket. It looks like this:
// INVALID JSON - trailing commas on both the object and the array
{
"name": "Alice",
"roles": [
"admin",
"editor", <-- trailing comma in array
], <-- trailing comma in object
}
When you try to parse this, you get an error like:
- JavaScript:
SyntaxError: Unexpected token } in JSON at position 42 - Python:
json.decoder.JSONDecodeError: Expecting value: line 6 column 3 (char 72) - Java Jackson:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)) - Go encoding/json:
invalid character '}' looking for beginning of value
The error message almost always points to the character after the trailing comma (the closing brace/bracket), not to the comma itself. This trips up many developers who look at the wrong position in the file.
Why Does JSON Forbid Trailing Commas?
JSON's grammar, defined in RFC 8259 and at json.org, defines an object as:
object = '{' [ member *( ',' member ) ] '}'
member = string ':' value
The grammar requires that every comma must be followed by another member. A comma after the last member with nothing following it is simply not part of the grammar. JSON was designed by Douglas Crockford to be a minimal, unambiguous data interchange format. Allowing trailing commas would have added parsing complexity with no real benefit - the format was deliberately kept strict.
JavaScript object literals and array literals do allow trailing commas (ES5+), which is why developers coming from a JS background often write them without thinking. But JSON is not JavaScript - it is a subset of JavaScript with a stricter grammar.
JavaScript allows trailing commas in object literals and arrays. JSON does not. Confusing the two is the root cause of most trailing comma errors.
Real Examples and What the Fix Looks Like
Trailing comma in an object
// BROKEN
{
"host": "localhost",
"port": 5432,
"database": "myapp", <-- trailing comma
}
// FIXED
{
"host": "localhost",
"port": 5432,
"database": "myapp" <-- comma removed
}
Trailing comma in an array
// BROKEN
{
"allowed_origins": [
"https://app.example.com",
"https://admin.example.com", <-- trailing comma
]
}
// FIXED
{
"allowed_origins": [
"https://app.example.com",
"https://admin.example.com" <-- comma removed
]
}
Trailing comma in nested structure
// BROKEN - trailing comma inside nested object
{
"user": {
"id": 42,
"name": "Alice", <-- trailing comma (inside nested object)
},
"token": "abc123"
}
// FIXED
{
"user": {
"id": 42,
"name": "Alice"
},
"token": "abc123"
}
Step-by-Step: How to Find and Fix the Error
- Read the error message carefully. The parser tells you the line and column number of the unexpected character. In most cases, the actual trailing comma is on the previous line - the error points to the closing brace that follows the comma.
- Go to that line minus one. If the error says line 8, check line 7 for a comma at the end.
-
Look for the pattern
,followed by only whitespace until a}or]. This is the classic trailing comma pattern. - Delete the trailing comma. Remove only the comma after the last element. Do not remove the element itself.
- Validate the result. Paste the fixed JSON into a validator to confirm it is now valid before using it.
Fix It Automatically: Regex Find-and-Replace
If you have a large JSON file with multiple trailing commas, fix them all at once with a regex in your editor (VS Code, Sublime, etc.):
// Find (regex):
,(\s*[}\]])
// Replace with:
$1
In VS Code: open Find & Replace (Ctrl+H / Cmd+H), enable the .* regex mode, enter the find pattern and the replacement, then click Replace All. This pattern matches any comma followed by optional whitespace and a closing brace or bracket, and removes the comma.
Caution: Always review the changes. Commas inside string values that happen to precede a } are extremely rare but could theoretically match. Run the regex, then validate the output.
Fix It with Command-Line Tools
# Using jq (parses and re-serializes, removing trailing commas as a side effect)
# jq is strict and will error if the JSON is invalid -- use Python instead for tolerant parsing:
python3 -c "
import json, sys, re
text = sys.stdin.read()
# Remove trailing commas before } or ]
fixed = re.sub(r',(\s*[}\]])', r'\1', text)
print(json.dumps(json.loads(fixed), indent=2))
" < broken.json > fixed.json
Fix JSON Errors Instantly Online
Paste your broken JSON and our formatter will detect trailing commas, missing quotes, and other syntax errors, showing you exactly what is wrong and producing a valid result.
Open JSON Formatter & ValidatorHow to Prevent Trailing Commas Going Forward
- Use a linter or formatter: ESLint with
"comma-dangle": ["error", "never"]flags trailing commas in JavaScript/TypeScript. Prettier removes them from JSON files automatically. - Use .jsonc for config files: The JSON with Comments format (.jsonc) is supported by VS Code and many tools. It allows trailing commas and comments, making config files much more maintainable. Keep API responses as strict JSON.
- Generate JSON programmatically: When building JSON in code, use your language's native serialization (e.g.,
JSON.stringify()in JavaScript,json.dumps()in Python). These always produce valid JSON and never add trailing commas. - Editor integration: Install the VS Code extension "JSON Tools" or enable built-in JSON validation. Red underlines on trailing commas appear immediately as you type.
- Pre-commit hooks: Run
python3 -m json.tool yourfile.jsonorjq . yourfile.jsonin a pre-commit hook to catch invalid JSON before it lands in version control.
Frequently Asked Questions
Why does my JSON work in JavaScript but fail in APIs and parsers?
JavaScript's JSON.parse() is strict and does not allow trailing commas. However, JavaScript object and array literals in source code do allow trailing commas because JS parsers are more lenient. If you copy a JS object literal into a context expecting JSON (an API request body, a config file, a database field), the trailing commas that were fine in JS source will cause a parse error in any strict JSON parser.
Does JSON5 allow trailing commas?
Yes. JSON5 is a superset of JSON that explicitly allows trailing commas, single-quoted strings, comments, and unquoted keys. Some configuration file formats (like .babelrc with Babel or VS Code's settings.json) use JSON5 or JSONC under the hood, which is why trailing commas work there. Standard JSON (RFC 8259) does not allow them.
Can I tell JSON.parse() to ignore trailing commas?
No, not the native JSON.parse(). It strictly follows the JSON specification. For lenient parsing that tolerates trailing commas, use a library like json5 in Node.js (JSON5.parse()) or jsonc-parser. These are appropriate for parsing user-authored config files but should never be used for data received from external APIs - use strict parsing there to catch real errors.
The error says line 1, but there's no comma on line 1. Why?
Minified JSON is a single line. If you have a compact JSON string with a trailing comma, the error position refers to the character offset within that single line, not a human-readable line number. Paste the JSON into a formatter first to expand it to multiple lines, then look for trailing commas in the expanded version.
My JSON validator says it's valid but my parser still fails. What's happening?
Two possibilities: (1) The validator is lenient (JSON5 mode) while your parser is strict. (2) There is a different syntax error beyond the trailing comma - perhaps a BOM character (byte order mark) at the start of the file, Windows-style CRLF line endings in a context expecting LF, or a Unicode control character inside a string. Use a strict JSON validator that shows all errors simultaneously.
Use Our Free JSON Formatter
The fastest way to fix a trailing comma error is to paste your JSON into our JSON Formatter. It highlights the exact location of every syntax error, including trailing commas, missing quotes, unescaped characters, and mismatched brackets. Once fixed, it formats the output with consistent indentation.
Use our free tool here → JSON Formatter at SecureBin.ai
Usman has 10+ years of experience securing enterprise infrastructure, managing high-traffic servers, and building zero-knowledge security tools. Read more about the author.