A trailing comma in JSON is a comma placed after the last item in an object or array, with nothing following it before the closing bracket. It's one of the most common JSON errors — and it's almost always caused by the fact that JavaScript allows trailing commas, but JSON does not.
What a trailing comma looks like
// This is INVALID JSON — trailing comma after "NYC"
{
"name": "Alice",
"city": "NYC",
}
// Also invalid in an array:
["red", "green", "blue",]
// Both are valid JavaScript, but will fail JSON.parse()
Why JSON forbids trailing commas
JSON is defined by RFC 8259. The grammar for an object is explicitly: a sequence of key-value pairs separated by commas, with no trailing comma allowed. The reason is simplicity: keeping the grammar unambiguous means any language can implement a JSON parser consistently without edge cases.
JavaScript (ES5 and later) allows trailing commas in object literals, array literals, and function parameters as a developer convenience — makes diffs cleaner when you add or remove a line. But JSON predates this and was intentionally kept stricter.
Where trailing commas come from
Copy-pasting from JavaScript source code
If you copy a JavaScript object literal and paste it as JSON — perhaps into a config file or an API testing tool — trailing commas come along with it.
Manual editing
You add a new field to a JSON object and forget to remove the comma from what is now the second-to-last field after rearranging. Or you delete the last field but leave its preceding comma on the field above.
// You had:
{
"host": "localhost",
"port": 5432,
"db": "mydb"
}
// You delete the last line:
{
"host": "localhost",
"port": 5432, // ↠now a trailing comma — easy to miss
}
Code generation bugs
If you're building JSON strings by concatenating items in a loop, a naive join can produce a trailing comma:
// Bug: produces trailing comma
const parts = items.map(i => `"${i.name}": ${i.value}`);
const json = '{' + parts.join(',') + ',}'; // extra comma before }
// Correct approach — always use JSON.stringify():
const obj = {};
items.forEach(i => { obj[i.name] = i.value; });
const json = JSON.stringify(obj);
How to find and remove trailing commas
The fastest way is to paste your JSON into the JSON Validator. It parses the input and reports the exact line and column where the trailing comma appears. For most text editors, you can then jump directly to that line.
If you need to strip trailing commas programmatically (for example, you're receiving JSON5-like input from a legacy system), you can use a regex as a pre-processing step before parsing. This is not recommended for production without thorough testing, but useful for one-off cleanup:
// Removes trailing commas before } or ]
// WARNING: This regex can fail on commas inside strings — use with care
function stripTrailingCommas(jsonString) {
return jsonString.replace(/,\s*([}\]])/g, '$1');
}
Prevent trailing commas upstream
The best fix is to never construct JSON by string concatenation. Always use JSON.stringify() in JavaScript or json.dumps() in Python. These serializers always produce valid JSON with no trailing commas.
// JavaScript — always produces valid JSON
const data = { name: "Alice", city: "NYC" };
const json = JSON.stringify(data, null, 2);
// Python — always produces valid JSON
import json
data = {"name": "Alice", "city": "NYC"}
json_str = json.dumps(data, indent=2)