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 1

The correct document is the same text with the two extra commas removed.

{
  "name": "ada",
  "roles": ["admin", "ops"]
}

Why it keeps happening

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

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