← All Articles
Developer Tools

Fix JSON Trailing Comma Errors: Why They Happen and How to Spot Them

JavaScript forgives a comma after the last item. JSON does not. That mismatch is behind a huge share of “unexpected token” parse errors, and it is easy to fix once you know where to look.

The NetTooling Team8 min read

You paste a config into a validator, hit parse, and get something like “Unexpected token } in JSON at position 184” or “Expected double-quoted property name.” Half the time the real villain is a trailing comma: a comma after the last property in an object or the last element in an array. Your eyes skip it because modern JavaScript and many editors treat trailing commas as good style. Standard JSON does not.

Why JSON rejects trailing commas

JSON is a data interchange format with a deliberately small grammar. After a value inside an object or array, a comma means “another value follows.” If the next character is } or ], the comma promised a sibling that never arrived, so parsers throw. ECMAScript object and array literals relaxed that rule years ago, which is why code that looks identical to JSON can still be invalid when you JSON.parse it or send it to an API that expects strict JSON.

The same trap appears when you copy from TypeScript/JavaScript source, from a README that shows “JSON-like” examples, or from a tool that pretty-prints with trailing commas enabled. YAML and some JSON5/JSONC dialects also allow them. Moving that text into a strict JSON field without scrubbing commas is a classic break.

What the error messages usually look like

  • Unexpected token } or Unexpected token ] right after a comma line.
  • Unexpected non-whitespace character after JSON at a trailing comma position.
  • Expected double-quoted property name — often the parser recovered badly after a comma and then choked on the closing brace.
  • Parse error near line N, where N is the closing bracket line, not the comma line above it.

How to spot the comma fast

Format the document first. Minified JSON hides trailing commas inside a wall of text; indented JSON puts the comma at the end of a line you can scan. Then jump to the position or line the parser reports and read upward. The illegal comma is often on the previous property or array item, while the reported token is the closing brace that followed it.

Search for patterns like ",}" and ",]" after formatting if your editor supports regex. Nested structures can contain several trailing commas; fix them from the inside out so each parse attempt moves the error to the next offender. A browser-based JSON formatter that validates locally is usually enough: paste, format or validate, fix the highlighted spot, repeat.

If JavaScript accepted the object literal but JSON.parse failed, check trailing commas before you rewrite the whole file.

Fixes and habits that prevent repeat failures

Delete the comma after the final property or element. Keep commas between siblings. If your team likes trailing commas in source code, keep that preference in .ts/.js files and generate strict JSON with JSON.stringify or a build step instead of hand-copying literals. For config files that must remain JSON, disable “trailing comma” formatting rules on those paths in Prettier or your editor.

When an API rejects a body, log the exact payload you sent. Middleware sometimes re-serializes objects safely, while a hand-built string template reintroduces commas and comments. If you need comments or trailing commas for human-edited configs, use JSONC in tooling that understands it, then emit strict JSON for runtime. Do not assume production parsers are lenient; many are not.

Related mistakes that look the same

Single quotes, unquoted keys, and comments produce similar “unexpected token” failures. Trailing commas are simply the most common when the document otherwise looks perfect. If removing commas does not help, validate again and check quote style next. A good formatter will refuse to pretty-print invalid input, which is a feature: it forces you to fix syntax before you trust the structure.

Frequently Asked Questions

Are trailing commas ever valid in JSON?

Not in standard JSON (RFC 8259). Some extensions like JSON5 or JSONC allow them, and JavaScript allows them in object/array literals, but JSON.parse and most APIs require strict JSON without trailing commas.

Why does the error point at } instead of the comma?

Parsers read the comma as a signal that another value is coming. When they hit } or ] instead, that closing token is what they report as unexpected. Look one line above the reported position.

How do I find trailing commas in a huge minified file?

Run it through a formatter or validator first. Once the structure is indented, trailing commas sit at the ends of lines and are easy to scan or regex-search as ",}" and ",]".

Can a JSON formatter fix trailing commas automatically?

Strict formatters usually refuse invalid JSON rather than silently rewriting it. Some “repair” tools can strip trailing commas; still review the result before shipping it to production.

Try the Tools

Put this into practice with our free, no-signup tools.