How to fix a trailing comma in JSON
JSON has no trailing commas. A comma directly before } or ] is a syntax error, so the whole document fails to parse even though every value in it is fine.
What the error looks like
{
"name": "ada",
"roles": ["admin", "ops",],
}
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 4 column 1The correct document is the same text with the two extra commas removed.
{
"name": "ada",
"roles": ["admin", "ops"]
}Why it keeps happening
- JavaScript allows it. Object and array literals in modern JavaScript accept a trailing comma, so hand-edited config copied out of code is often invalid JSON.
- Templates append commas. Generating a list by writing
item + ","leaves one comma too many after the last item. - Deleting the last entry. Removing the final key of an object leaves the comma that used to separate it from the one before.
Fixing it safely
The obvious repair, a search and replace for ,} and ,], is unsafe: those two characters can appear inside a string value, and a plain replacement silently corrupts the data. A correct repair has to track whether it is inside a string and whether the previous character was an escape.
That is exactly what the free JSON Repair tool does. It removes commas that sit before a closing brace or bracket outside of strings, drops a leading byte-order mark, parses the result strictly, and prints it back with two-space indentation. If the document is still invalid it reports the line and column and changes nothing, because guessing at a repair is how data gets lost.
What it deliberately will not do
- It does not add missing quotes, brackets, or commas.
- It does not accept comments, single-quoted strings, or unquoted keys.
- It rejects
NaNandInfinity, which are not valid JSON. - It takes up to 32 KB of input per run.
Every one of those is a case where more than one output would be defensible, and a tool that picks one for you produces a file that parses but no longer means what it did.
Next
- Repair one document now, free and without an account.
- Fixing a folder rather than a file? See repairing many broken JSON files in one pass.