Skip to content
Debugging

Fix Trailing Comma in JSON: Quick Guide

·7 min read

You just spent twenty minutes building a JSON config file by hand. You save it. You run your app. And the parser throws this at you:

SyntaxError: Expected double-quoted property name in JSON at position 47

Position 47. What's at position 47? You squint at the file. Everything looks fine. The keys are quoted. The brackets match. The values are correct. So what's the problem?

It's a trailing comma. One tiny comma after the last item in an object or array - and your entire JSON file is broken.

If you've ever searched for "trailing comma JSON error" or "JSON trailing comma not allowed," you're in the right place. Let's fix this for good.

What Does the Trailing Comma Error Actually Look Like?

A trailing comma is a comma that appears after the last element in an object or array. Here's a simple example that will break every JSON parser on the planet:

{
  "name": "Alex",
  "role": "developer",
  "active": true,
}

See that comma after true? That's the problem. The parser reads the comma and expects another key-value pair to follow. Instead it finds a closing brace. Crash.

The same thing happens in arrays:

{
  "tags": ["frontend", "react", "typescript",]
}

That comma after "typescript" is invalid. Remove it, and the JSON parses perfectly.

Different parsers will give you different error messages for this. You might see:

They all mean the same thing. There's a comma where there shouldn't be one.

Why JavaScript Allows Trailing Commas but JSON Doesn't

This is the part that trips up JavaScript developers constantly. In JavaScript, trailing commas are not just allowed - they're encouraged. The ECMAScript spec has supported trailing commas in array literals since ES3 (1999) and in object literals since ES5 (2009). Most linters like ESLint even have rules that enforce trailing commas in your code.

So your muscle memory is trained to add them. You write JavaScript objects all day with trailing commas, and then you open a JSON file and do the exact same thing. Except JSON is not JavaScript.

JSON is a data format defined by RFC 8259. It borrowed its syntax from JavaScript, but it's deliberately stricter. Douglas Crockford, who formalized JSON, left out trailing commas on purpose. The goal was to keep JSON as simple and unambiguous as possible - no room for parser disagreements, no edge cases.

The result? Every JSON parser in every language rejects trailing commas. No exceptions. It doesn't matter if you're using Python, Go, Java, Ruby, or Rust. A trailing comma is always invalid JSON.

How to Fix It - Before and After

The fix is dead simple. Remove the trailing comma. Here's what broken JSON looks like versus the fixed version:

Trailing Comma in an Object

// BROKEN - trailing comma after the last property
{
  "database": "postgres",
  "host": "localhost",
  "port": 5432,
}

// FIXED - no comma after the last property
{
  "database": "postgres",
  "host": "localhost",
  "port": 5432
}

Trailing Comma in an Array

// BROKEN - trailing comma after last array element
{
  "colors": ["red", "green", "blue",]
}

// FIXED - no comma after last array element
{
  "colors": ["red", "green", "blue"]
}

Nested Trailing Commas

The tricky cases are nested structures where trailing commas hide at multiple levels:

// BROKEN - trailing commas at two levels
{
  "user": {
    "name": "Alex",
    "preferences": {
      "theme": "dark",
      "language": "en",
    },
  }
}

// FIXED - both trailing commas removed
{
  "user": {
    "name": "Alex",
    "preferences": {
      "theme": "dark",
      "language": "en"
    }
  }
}

In that example, there were two trailing commas - one after "en" and one after the closing brace of preferences. Miss either one and the JSON still won't parse.

How to Spot Trailing Commas in Large JSON Files

Finding a trailing comma in a 20-line config is easy. Finding one in a 2,000-line API response? That's a different story. Here are the approaches that actually work:

Use a JSON Validator

The fastest method. Paste your JSON into a validator like JSON Prettifier and it will highlight the exact line where the error occurs. No manual searching required.

Use Your Editor

VS Code highlights JSON syntax errors in real time when you're working in a .jsonfile. Look for red squiggly underlines - they'll point you right to the trailing comma. Make sure your file is saved with a .json extension so VS Code applies JSON validation.

Use a Regex Search

If you need to find trailing commas across multiple files, a regex search can help. In VS Code or your terminal, search for:

,\s*[\]\}]

This pattern matches a comma followed by optional whitespace and then a closing bracket or brace - which is exactly what a trailing comma looks like. It's not perfect (it could match inside strings), but it catches the vast majority of cases.

How to Prevent Trailing Commas in JSON

Fixing trailing commas one at a time is fine. Preventing them from happening in the first place is better. Here's how to do that.

Generate JSON Programmatically

The most reliable prevention method is to stop writing JSON by hand. Use JSON.stringify() in JavaScript, json.dumps() in Python, or the equivalent in your language. These functions produce valid JSON by construction - they will never add a trailing comma.

// JavaScript - always produces valid JSON
const config = {
  database: "postgres",
  host: "localhost",
  port: 5432
};

const json = JSON.stringify(config, null, 2);
// No trailing commas, guaranteed

Configure Your Editor

If you use VS Code, make sure your JSON formatting settings don't add trailing commas. Check that you have separate formatter settings for .json files versus .js or .ts files. The Prettier extension, for example, respects the trailingComma config for JavaScript but correctly omits trailing commas in JSON files.

Use a Pre-Commit Hook

Add a JSON validation step to your Git pre-commit hooks. Tools like jsonlint or prettier --check can catch invalid JSON before it ever reaches your repository. If someone accidentally adds a trailing comma to a config file, the commit gets rejected with a clear error message.

# Install jsonlint globally
npm install -g jsonlint

# Validate a JSON file
jsonlint config.json

Let the Tool Catch It for You

You don't need to memorize any of this. Paste your broken JSON into JSON Prettifier and it will immediately flag the trailing comma, show you exactly where it is, and let you fix it with one click. The validator catches trailing commas, missing quotes, mismatched brackets, and every other common JSON syntax error automatically.

If you're running into other JSON issues beyond trailing commas, check out our full guide on common JSON errors and how to fix them. And for a deeper dive into working with JSON programmatically, our guide on JSON in JavaScript covers JSON.parse(), JSON.stringify(), and all the edge cases you need to know.

Fix Your JSON in Seconds

Paste your broken JSON into our validator. It catches trailing commas, missing quotes, and every other syntax error - instantly.

Open JSON Prettifier