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.
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 JSON Formatter
Format, beautify and indent JSON payloads
Open →
JSON Validator
Line-by-line syntax diagnostics & error locator
Open →
JSON Viewer
Interactive tree explorer with path inspection
Open →
JSON Repair
Fix trailing commas, quotes and unquoted keys
Open →
JSON Diff
Semantic comparison ignoring key order
Open →
JSONPath Evaluator
Query, extract and filter JSON structures
Open →
JSON Minifier
Compress and compact JSON into single line
Open →
JSON to TypeScript
Generate typed TypeScript interfaces
Open →
JSON to Python
Generate Pydantic v2 & TypedDict models
Open →
JSON to Go
Generate idiomatic Go structs with json tags
Open →
JSON to Rust
Generate type-safe Serde structs
Open →
JSON to SQL
Generate CREATE TABLE & INSERT statements
Open →
JSON to CSV
Convert tabular JSON arrays to standard CSV
Open →
JSON to YAML
Clean converter for configuration files
Open →