Cryonel

JSON Trailing Comma Error, Explained

JSON does not allow a comma after the last element of an object or array. It's a strict, deliberate part of the spec — not a parser bug.

What triggers it

{
  "name": "Ada",
  "role": "admin",
}

That trailing comma after "admin" is invalid. The same applies to arrays: [1, 2, 3,] fails to parse.

Why JSON is stricter than JavaScript here

JavaScript object and array literals do allow a trailing comma — it was added specifically so version control diffs stay clean when you add a new last item. JSON, defined independently as a data interchange format (RFC 8259), never adopted that allowance. The two look similar but are governed by different grammars, and this is the most common place the difference bites people who write JSON by hand or generate it with string templates.

How to avoid it

Diagnose the source before repairing the file

If the document came from an API, configuration generator, or template, fix that producer instead of repeatedly cleaning its output. Log the parser's line and column, inspect the property immediately before that location, and compare the raw response with the value shown by an editor. Browser extensions, log formatters, and copy-paste can change whitespace, so keep one untouched sample for the regression test.

A global regular expression such as “comma followed by a closing brace” is risky because JSON strings may contain braces, commas, and escaped quotes. A parser-aware repair step understands whether a character is data or syntax. After repairing, parse the result again and compare the decoded value with the expected object; syntactically valid output can still be semantically wrong if a field was removed.

Frequently Asked Questions

Can JSON contain trailing commas?

No. The final property or array item must not be followed by a comma.

Why does the same object work in JavaScript?

JavaScript literals allow syntax that the independent JSON grammar forbids.

Should I strip commas with a regular expression?

Use a parser-aware repair tool so text inside quoted strings is not changed accidentally.

Related Tools and Guides