{FormJSON}
Developer Syntax Guide • RFC 8259 Specification

How to Fix Trailing Commas in JSON

Why trailing commas break JSON.parse(), json.loads(), and json.Unmarshal, and how to strip them automatically.

Remove trailing commas automatically
FormJson's AST Safe Repair engine detects and cleans trailing commas without breaking nested structures.
Clean Trailing Commas →

01. Valid vs. Invalid Comma Placement

In standard JSON, commas are strictly element separators, never terminators. Placing a comma before a closing brace } or bracket ] violates the grammar specification.

✕ Invalid JSON (Trailing Commas)
{
  "project": "FormJson",
  "version": "1.0.0",   // 💥 Trailing comma in object
}

[
  "format",
  "repair",            // 💥 Trailing comma in array
]
✓ Valid RFC 8259 JSON
{
  "project": "FormJson",
  "version": "1.0.0"
}

[
  "format",
  "repair"
]

02. Programmatic Solutions

If you are processing LLM outputs, configuration files, or user inputs that may contain trailing commas, you can sanitize them before parsing:

JavaScript / Node.js Regex Cleaner:
function stripTrailingCommas(jsonString) {
  return jsonString.replace(/,\s*([\}\]])/g, '$1');
}

const cleaned = stripTrailingCommas('{"a": 1, "b": 2,}');
const parsed = JSON.parse(cleaned); // { a: 1, b: 2 }
Python Regex Cleaner:
import re
import json

def parse_with_trailing_commas(json_str):
    cleaned = re.sub(r',\s*([\}\]])', r'\1', json_str)
    return json.loads(cleaned)

Frequently Asked Questions

FAQ

Why does standard JSON forbid trailing commas?
The original JSON specification created by Douglas Crockford (and formalized in RFC 8259 / ECMA-404) designed JSON as a minimal, strict data interchange format. Trailing commas were omitted to ensure simple parsing across all programming languages.
Why do JavaScript and TypeScript allow trailing commas but JSON doesn't?
ECMAScript allows trailing commas in object literals and arrays for cleaner Git diffs. However, standard JSON is a subset of JavaScript and does not support ECMAScript 5+ trailing comma extensions.
How can I strip trailing commas automatically?
Use FormJson's Safe Repair tool (/json-repair) to automatically remove trailing commas from objects and arrays with a single click, or use JSON5 / JSONC in environments that support extended JSON formats.
Does Python json.loads() support trailing commas?
No. Python's standard library 'json' module adheres strictly to RFC 8259 and will raise 'json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes' if a trailing comma is present.

Explore Related Tools & Converters

100% Client-Side