Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

Handling Trailing Commas in JSON: How to Remove the Extra Comma Safely

If you are trying to remove a comma from a JSON object or array, the rule is simple: standard JSON does not allow a comma immediately before a closing } or ]. Delete that final comma, then validate the document again. Most formatters and parsers will reject the payload until that syntax error is fixed.

Quick answer

  • A trailing comma is an extra comma before } or ].
  • In strict JSON, {"name":"Ada",} and ["a","b",] are invalid.
  • If you see an error like Unexpected token } or Unexpected token ], check the character just before the closing brace or bracket.
  • If a tool accepts trailing commas, it is usually reading a relaxed format such as JSON5 or JSONC, not standard JSON.

What Counts as a Trailing Comma?

A trailing comma, also called a dangling comma, appears after the last property in an object or the last element in an array. That final comma is valid in many JavaScript contexts, but not in JSON.

Invalid JSON

{
  "name": "Ada",
  "role": "Engineer",
}

[
  "apple",
  "banana",
  "orange",
]

In both examples, the last comma appears right before the closing character. That is the part you remove.

Valid JSON

{
  "name": "Ada",
  "role": "Engineer"
}

[
  "apple",
  "banana",
  "orange"
]

Why JSON Rejects the Extra Comma

The current JSON standard is RFC 8259. Its grammar allows a comma only between members or array values, never after the last one. MDN's current JSON.parse() reference also notes that arrays and objects with trailing commas throw a SyntaxError.

  • Interoperability: Strict grammar keeps parsers aligned across languages and platforms.
  • Predictable parsing: A comma always means another value is coming next.
  • Clear boundaries: JSON intentionally omits some JavaScript conveniences, including comments and trailing commas.

Important distinction

JavaScript object literals often allow trailing commas. JSON does not. Copying a JavaScript object directly into a JSON file is one of the most common ways this error gets introduced.

How to Remove a Trailing Comma From JSON

If your goal is simply to make the JSON valid again, use this order of operations:

  1. Find the closing } or ] where parsing fails.
  2. Check the previous non-whitespace character.
  3. If that character is a comma, delete it.
  4. Run the JSON through a validator or formatter again to confirm there are no other syntax errors.

Before and after

Before

{
  "config": {
    "enabled": true,
    "features": ["search", "export", "import"],
  }
}

After

{
  "config": {
    "enabled": true,
    "features": ["search", "export", "import"]
  }
}

What a JSON Formatter Should Do

A good formatter should help you fix trailing commas quickly instead of hiding the problem.

  • Reject invalid JSON instead of pretty-printing it as if it were correct.
  • Point to the exact line and column near the closing brace or bracket.
  • Offer a cleanup step only if it is explicit about changing the input.
  • Re-validate after cleanup so the output is strict JSON, not just nicer-looking text.

Common Ways the Problem Gets Introduced

Trailing commas usually show up for boring, fixable reasons:

  • Manual edits

    You delete the last item in a list or object member list and leave its comma behind.

  • Copy-paste from JavaScript

    JavaScript literals often include syntax that JSON forbids, especially comments and trailing commas.

  • Templating or string concatenation

    Hand-built JSON strings often add commas after every item instead of only between items.

  • Confusing config formats with JSON

    Some developer tools accept relaxed formats like JSON5 or JSONC, which can make invalid JSON look normal.

Best Practices

1. Validate at the boundary

Validate payloads before they hit APIs, databases, queues, or config loaders. That catches trailing commas before they become production failures.

2. Generate JSON with serializers

When your code can build a native object or array and serialize it, do that instead of hand-writing JSON strings.

3. Keep relaxed formats out of strict JSON pipelines

JSON5 and JSONC are useful for human-edited config files, but they should be converted to strict JSON before they are sent to systems that expect standard JSON.

4. Do not rely on naive regex cleanup

Replacing ,} with } and ,] with ] can break valid string content. Fix the producer or use a parser designed for relaxed JSON syntax.

When JSON5 or JSONC Make Sense

If the input is a developer-facing configuration file and you intentionally want comments or trailing commas, use a relaxed parser first and then serialize back to strict JSON if another system needs standard output.

Example normalization flow

import JSON5 from "json5";

const relaxedInput = `{
  "name": "project",
  "features": ["search", "export",],
}`;

const normalized = JSON.stringify(JSON5.parse(relaxedInput), null, 2);
console.log(normalized);

Use this only when you intentionally accept a relaxed format. For external data exchange, strict JSON is the safer default.

Troubleshooting Checklist

If the trailing comma error keeps coming back, check these points:

  • The payload is actually JSON, not a JavaScript object literal or another config format.
  • The parser error points near a closing brace or bracket, not somewhere earlier in the file.
  • There are no comments in the document. Comments are also invalid in standard JSON.
  • The JSON is being generated with a serializer rather than assembled by string concatenation.
  • The system reading the file expects strict JSON and is not documented as accepting JSON5 or JSONC.

Conclusion

Trailing commas are easy to fix once you know what to look for: remove the comma directly before the closing } or ], then validate again.

The current standard in RFC 8259 still forbids trailing commas, and current MDN documentation for JSON.parse() still documents them as a SyntaxError. Use relaxed formats only when you control the environment and explicitly need them.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool