Trailing commas in JSON
JSON doesn’t allow a comma after the last item in an object or array. {"a": 1,} and [1, 2,] are both invalid, even though JavaScript accepts them. Paste your JSON below to remove them.
Your JSON
Why JSON rejects trailing commas
In the JSON grammar, a comma separates two items. After a comma the parser expects another property or value, so when it finds } or ] instead, it stops with an error. The same comma is fine between items: only the one before the closing bracket has to go.
Invalid
{
"name": "Demo",
"tags": ["api", "json",],
}Valid
{
"name": "Demo",
"tags": ["api", "json"]
}JSON’s syntax was defined in the early 2000s from a subset of JavaScript, before JavaScript allowed trailing commas in object literals. The grammar has never changed since, and the current standards, RFC 8259 and ECMA-404, still leave them out.
The error you’ll see
Few parsers mention the comma. They report the character after it, so look just before the reported position.
| Parser | Object: {"a": 1,} | Array: [1, 2,] |
|---|---|---|
| JavaScript (Chrome, Node.js) | Expected double-quoted property name in JSON at position 8 | Unexpected token ']', "[1, 2,]" is not valid JSON |
| Python | Expecting property name enclosed in double quotes: line 1 column 9 (char 8) | Expecting value: line 1 column 7 (char 6) |
| Go (encoding/json) | invalid character '}' looking for beginning of object key string | invalid character ']' looking for beginning of value |
| Ruby | JSON::ParserError: unexpected token | JSON::ParserError: unexpected token |
| PHP | json_decode() returns null; json_last_error_msg() is "Syntax error" | Same |
Other parse errors look similar, and the JSON errors reference covers the rest.
Where trailing commas come from
- Deleting the last item. Remove the final property by hand and the comma on the line above stays behind.
- Copying from code. JavaScript and TypeScript objects often end with a comma, especially with code formatters set to add them.
- Building JSON as text. A loop that appends
item + ","leaves one extra comma at the end. - Config files that allow them.
tsconfig.jsonand VS Code’ssettings.jsonaccept trailing commas, so the habit carries over topackage.json, which doesn’t.
Removing them in code
Use a parser rather than find-and-replace. A regular expression such as ,\s*([}\]]) works on simple files, but it also changes text inside strings, so a value like "a ,]" would be edited too.
In JavaScript, jsonrepair (the library behind the validator’s Fix My JSON button) returns valid JSON text. JSON5 parses trailing commas directly:
import { jsonrepair } from "jsonrepair";
const data = JSON.parse(jsonrepair(text));
// or
import JSON5 from "json5";
const data = JSON5.parse(text);In Python, the json5 package reads them:
import json5 # pip install json5
data = json5.loads(text)From the command line, jsonrepair and Prettier can both fix a file. Prettier removes trailing commas whenever it formats a .json file:
npx jsonrepair broken.json --output fixed.json
npx prettier --write settings.jsonAvoiding them
Generate JSON with JSON.stringify in JavaScript or json.dumps in Python instead of writing it by hand. They never add trailing commas. If you do have to build text, collect the parts in a list and join them with commas, so the separator only goes between items. For hand-edited files, an editor that understands JSON underlines the stray comma, and Prettier removes it every time the file is saved.
Frequently asked questions
Is a trailing comma valid in JSON?
No. The JSON standard only allows commas between items, so any comma directly before } or ] makes the document invalid, and standard parsers reject it.
Can package.json have trailing commas?
No. npm reads package.json as strict JSON, so a trailing comma makes npm stop with an EJSONPARSE error. The message ends with “package.json must be actual JSON, not just JavaScript.”
Why does tsconfig.json accept trailing commas?
TypeScript reads its config files with its own parser, which accepts comments and trailing commas. VS Code settings files work the same way. Both are “JSON with comments” (JSONC), not strict JSON.
Does JSON5 allow trailing commas?
Yes. JSON5 is a separate format that extends JSON with trailing commas, comments, single quotes, and unquoted keys. It needs a JSON5 parser; JSON.parse and other standard parsers still reject it.
Why does JavaScript allow trailing commas when JSON doesn’t?
They make it easier to add, remove, and reorder lines, and they keep version-control diffs to one line. JavaScript added them to object literals in 2009, but JSON’s grammar was already fixed and never changed.