JSON

Trailing Comma in JSON: Why It Breaks Your Data

📅 December 14, 2025 ⏱️ 2 min read 👁️ 10 views 🏷️ JSON

A trailing comma is one of the most frequent causes of JSON parse errors. While JavaScript and many programming languages allow trailing commas, JSON does not. This guide explains why and shows you how to fix it.

What is a Trailing Comma?

A trailing comma is a comma after the last element in an array or the last property in an object:


// INVALID JSON - trailing comma in object
{
  "name": "John",
  "age": 30,    // <- This comma breaks JSON
}

// INVALID JSON - trailing comma in array
[
  "apple",
  "banana",
  "orange",    // <- This comma breaks JSON
]

Why JSON Does Not Allow Trailing Commas

JSON is a strict data format based on a subset of JavaScript. The JSON specification (RFC 8259) explicitly forbids trailing commas to keep parsing simple and unambiguous.


// This is valid JavaScript but INVALID JSON
const jsObject = {
  name: "John",
  age: 30,  // OK in JavaScript
};

// Valid JSON requires no trailing comma
{"name": "John", "age": 30}

Common Scenarios That Create Trailing Commas

1. Copy-Paste from Code


// Developer copies JavaScript object and forgets to clean it
const config = {
  debug: true,
  port: 3000,  // Trailing comma - valid in JS
};

// When saved as config.json, this breaks

2. Removing Last Item


// After removing "cherry", developer forgets to remove comma
{
  "fruits": [
    "apple",
    "banana",   // <- Now a trailing comma!
  ]
}

3. Auto-Generated JSON


# Poor code that generates trailing commas
items = ["a", "b", "c"]
json_str = "{" + ", ".join([f'"{i}"' for i in items]) + ",}"  # Wrong!

# Correct approach
import json
json_str = json.dumps({"items": items})

How to Find and Fix Trailing Commas

Paste your JSON into our JSON formatter online and it will instantly highlight trailing comma errors with the exact line number.

Manual Fix


// Before (invalid)
{
  "name": "John",
  "email": "john@example.com",
}

// After (valid)
{
  "name": "John",
  "email": "john@example.com"
}

Regex to Find Trailing Commas


// Find trailing commas before closing brackets
const trailingCommaRegex = /,\s*([}\]])/g;
const fixed = jsonString.replace(trailingCommaRegex, '$1');

Python Script to Fix


import re

def remove_trailing_commas(json_str):
    # Remove trailing commas before } or ]
    pattern = r',\s*([}\]])'
    return re.sub(pattern, r'', json_str)

# Usage
fixed_json = remove_trailing_commas(broken_json)

Editor Settings to Prevent Trailing Commas

VS Code


// settings.json
{
  "json.format.enable": true,
  "editor.formatOnSave": true
}

ESLint (for JavaScript)


{
  "rules": {
    "comma-dangle": ["error", "never"]
  }
}

Key Takeaways

  • JSON never allows trailing commas
  • JavaScript allows them, causing confusion
  • Always validate JSON before using in production
  • Use a formatter to catch errors instantly

Use our JSON formatter online to validate your JSON and automatically format it correctly.

🏷️ Tags:
trailing comma json json comma error fix json comma json syntax

📚 Related Articles