toolhq.io

All posts
5 min readby Jameel Haider

Trailing commas in JSON: why they break parsing and how to fix it

A single comma after the last element is the most common reason a hand edited JSON file refuses to parse. The value is deleted, the comma stays, and the whole document is now invalid. JavaScript object literals forgive this. JSON does not, and the mismatch catches people daily.

What the spec actually says

The JSON grammar defines an array as elements separated by commas and an object as members separated by commas. The key word is separated. A comma sits between two values, never after the final one. There is no production in the grammar that allows a comma followed by ] or }. This is not a stylistic preference, it is the formal syntax, so every conforming parser must reject it.

{
  "name": "server",
  "port": 8080,
}

That trailing comma after 8080 makes the object invalid.

The error you get

The message depends on the runtime, but all of them point at the closing bracket:

RuntimeError
JSON.parse (V8)SyntaxError: Unexpected token } in JSON
Python json.loadsExpecting property name enclosed in double quotes
Go encoding/jsoninvalid character '}' looking for beginning of object key string

Python's wording is the most confusing. It does not mention commas at all. The parser consumed the comma, decided another member must follow, and then found a } where it wanted a key. The cause is still the trailing comma.

Why JavaScript misleads you

Modern JavaScript allows trailing commas in array and object literals, function calls, and parameter lists. They make diffs cleaner because adding a line does not touch the previous line. People carry that habit into JSON files and assume the same rules apply. They do not. JSON is a strict subset defined independently of JavaScript, and it predates the trailing comma allowance in the language.

JSON5 and JSONC do allow them

Two common supersets relax the rule on purpose:

  • JSON5 permits trailing commas, comments, unquoted keys, and single quotes.
  • JSONC (JSON with Comments) permits trailing commas and comments. VS Code settings.json and TypeScript tsconfig.json both use it.

The catch is that this only helps when the tool reading the file actually uses a JSON5 or JSONC parser. A trailing comma in tsconfig.json is fine because TypeScript expects it. The same comma in package.json breaks npm, because npm reads strict JSON. The file extension tells you nothing. Know which parser consumes the file.

Stripping trailing commas safely

You may be tempted to remove them with a quick regex such as replace(/,(\s*[}\]])/g, '$1'). This works until a comma followed by whitespace and a bracket appears inside a string value, for example "note": "use this, or that]". A naive regex will corrupt that string. For anything beyond throwaway code, use a real JSON5 parser to read the loose input, then re-serialize with JSON.stringify to get valid strict JSON back out. That round trip removes trailing commas and comments correctly because the parser understands string boundaries.

Quick rules

  • Strict JSON for data interchange and anything a third party parses: never a trailing comma.
  • Config files your own tooling reads: switch to a JSON5 or JSONC parser if you want the convenience.
  • Never fix loose JSON with a bare regex in production.

If your document fails to parse and you cannot see why, the JSON Formatter runs entirely in your browser and points at the exact line and column where the stray comma sits.