DevKitLab
JSON
2026-11-09
•
7 min read
•
Fact-Checked & RFC Verified

Why Trailing Commas Are Forbidden in JSON: RFC 8259 vs. JSON5 & JSONC Explained

Why does JavaScript allow trailing commas while strict JSON rejects them? Compare RFC 8259, ECMA-404, JSONC, and JSON5 with practical repair examples.

Sponsored Developer RecommendationAdSense Compliant Slot
JSON Developer Architecture Reference100% Client-Side Privacy

Comprehensive engineering standards, security safeguards, and client-side processing insights. Bookmark DevKitLab for rapid, zero-login daily engineering workflows.

In modern JavaScript, TypeScript, Python, and Rust, adding a trailing comma (also called a dangling comma) after the final item in an array or object is considered a best practice because it keeps git diff output clean when adding new lines.

However, the moment you add a trailing comma to a standard .json file or REST API body, production parsers throw a fatal syntax error:

json
{
  "service": "payment-worker",
  "retries": 3,
}
text
SyntaxError: Expected double-quoted property name in JSON at position 45

Why RFC 8259 and ECMA-404 Forbid Trailing Commas

When Douglas Crockford specified JSON in the early 2000s (later standardized as IETF RFC 8259 and ECMA-404), the primary design goal was minimal, unambiguous grammar that could be implemented in a few hundred lines of C, Java, Python, or PHP without version negotiation.

In the formal RFC 8259 Section 4 grammar, an object is defined strictly as:

text
object = begin-object [ member *( value-separator member ) ] end-object

Every value-separator (,) must be followed by another member ("key": value). A closing brace } or bracket ] immediately after a comma violates the state transition table of compliant parsers.


Strict JSON (RFC 8259) vs. JSONC vs. JSON5

Why do files like VS Code's settings.json or TypeScript's tsconfig.json accept comments and trailing commas? Because they do not use strict JSON—they use JSONC (JSON with Comments) or JSON5.

FeatureStrict JSON (RFC 8259)JSONC (VS Code / tsconfig)JSON5
Trailing Commas❌ Forbidden✅ Allowed✅ Allowed
*// and `/ /` Comments*❌ Forbidden✅ Allowed✅ Allowed
Unquoted Object Keys❌ Forbidden❌ Forbidden✅ Allowed
Single-Quoted Strings❌ Forbidden❌ Forbidden✅ Allowed
Hexadecimal Numbers❌ Forbidden❌ Forbidden✅ Allowed

How to Safely Remove Trailing Commas Without Corrupting Strings

Using a naive global regular expression like str.replace(/,\s*}/g, "}") in production code is dangerous if a string literal inside your JSON happens to contain the characters ", }".

When debugging or migrating JSON5/JSONC config files to strict RFC 8259 JSON:

  1. Paste your payload into the DevKitLab JSON Formatter.
  2. Our engine runs strict JSON.parse() first and flags the exact line and column of the trailing comma.
  3. Review the Proposed JSON Repair Preview—which lists every removed trailing comma, comment, or single quote—and click Accept Repair & Process only after verifying the diff.
Sponsored Developer RecommendationAdSense Compliant Slot
DevKitLab Interactive Utilities100% Client-Side Privacy

Explore our 44+ instant, client-side developer formatters and decoders without accounts or subscriptions. Bookmark DevKitLab for rapid, zero-login daily engineering workflows.

Interactive Tools for This Guide

Frequently Asked Questions

Can I use comments in package.json?

No. Node.js and npm parse package.json using strict JSON.parse(), which rejects // and /* */ comments. Only tsconfig.json and VS Code configuration files use JSONC.

Does removing trailing commas change array length?

In JSON objects and arrays, removing a trailing comma before } or ] produces a valid RFC 8259 structure with the exact same elements.

More Developer Guides & Tutorials