What is a trailing comma in JSON?
A trailing comma is a comma after the last element of an object or array, right before } or ]:
{ "build": "vite build", "test": "vitest", } ← invalid JSON
[ "json", "tools", ] ← invalid JSON
{ "build": "vite build", "test": "vitest" } ← valid
JavaScript, TypeScript, Python, Go and Rust all allow this in source code, so it is an easy habit to carry into a JSON file. Strict JSON parsers reject it, which is why a config file that "looks fine" breaks a build, and why npm install fails on a package.json with a stray comma.
Why JSON forbids trailing commas
The JSON grammar (RFC 8259 and ECMA-404) defines a comma as a separator between two values, so there is nothing for a final comma to separate. JSON was specified in the early 2000s from a subset of JavaScript at a time when old browsers handled trailing commas inconsistently (older Internet Explorer gave arrays an extra empty element), and the grammar has deliberately never changed since. Every compliant parser has to reject them, which is what keeps JSON portable between languages.
Unexpected token } in JSON at position N and other trailing comma messages
The message depends on the parser, and some do not mention commas at all. This table lists what each one says for {"a":1,} and [1,2,]:
| Language / parser | Object: {"a":1,} | Array: [1,2,] |
|---|---|---|
| JavaScript, current V8 (Chrome, Edge, Node.js 20+) | Expected double-quoted property name in JSON at position 7 | Unexpected token ']', "[1,2,]" is not valid JSON |
| JavaScript, older V8 (Node 18 and earlier) | Unexpected token } in JSON at position 7 | Unexpected token ] in JSON at position 5 |
| Firefox | JSON.parse: expected double-quoted property name at line 1 column 8 of the JSON data | JSON.parse: unexpected character at line 1 column 6 of the JSON data |
| Python 3.12 and earlier | Expecting property name enclosed in double quotes: line 1 column 8 (char 7) | Expecting value: line 1 column 6 (char 5) |
| Python 3.13+ | Illegal trailing comma before end of object: line 1 column 7 (char 6) | Illegal trailing comma before end of array: line 1 column 5 (char 4) |
Go encoding/json | invalid character '}' looking for beginning of object key string | invalid character ']' looking for beginning of value |
| jq 1.7 | jq: parse error: Expected another key-value pair at line 1, column 8 | jq: parse error: Expected another array element at line 1, column 6 |
| Java (Jackson) | Unexpected character ('}' (code 125)): was expecting double-quote to start field name | — |
PHP json_last_error_msg() | Syntax error | Syntax error |
Expecting property name enclosed in double quotes (Python)
In Python 3.12 and earlier, a trailing comma in an object gives this confusing message: after the comma, json.loads expects another "key" and finds }. The same message also appears for single-quoted keys ({'a': 1}, usually from str(dict)) and unquoted keys, so look at the character at the reported column. If it is }, delete the comma before it. If it is ', see Python dict to JSON. Python 3.13 and later say Illegal trailing comma before end of object instead, which points at the comma itself.
Where trailing commas are allowed: JSONC, JSON5 and JavaScript
- JSON5 allows trailing commas, comments, single quotes and unquoted keys. Parse it with the
json5package; check a file with the JSON5 validator. - JSONC ("JSON with Comments") is the format VS Code uses for
settings.json,keybindings.jsonand similar files, and TypeScript accepts comments and trailing commas intsconfig.json. These tools use their own tolerant parsers. In VS Code you can switch a file to JSONC with the language picker in the status bar, or map file names in settings with"files.associations": { "*.myconfig.json": "jsonc" }. More in the JSON comments guide. - JavaScript object literals allow them (objects since ES5, arrays always). A
.jsconfig file such aseslint.config.jscan have them; a.jsonfile cannot. To turn an object literal into JSON, use JavaScript object to JSON. - Jackson (Java) accepts them if you enable
JsonReadFeature.ALLOW_TRAILING_COMMA.
Files read by strict tools must stay strict: package.json, composer.json, API request and response bodies, and anything parsed by JSON.parse, json.loads, jq or Go.
How to remove trailing commas: regex vs parser
A common one-liner removes a comma followed by optional whitespace and a closing bracket:
text.replace(/,\s*([}\]])/g, '$1') // JavaScript
re.sub(r',\s*([}\]])', r'\1', text) # Python
It works on most config files, but it cannot tell whether it is inside a string. On the third example above, "note": "keep this, }" becomes "keep this }" and silently changes your data. It also misses commas followed by a comment, such as "a": 1, // last.
A parser does not have these problems because it knows where strings start and end. Repair JSON above uses NeatJSON's parser: it removes trailing and doubled commas, leaves string contents untouched, and lists every fix with its line and column. In code, use a tolerant parser and write strict JSON back out:
// Node.js: npm install json5
const JSON5 = require('json5');
const strict = JSON.stringify(JSON5.parse(text), null, 2);
# Python: pip install json5
import json, json5
strict = json.dumps(json5.loads(text), indent=2)
Be aware that JSON.parse and JSON5.parse in JavaScript turn numbers into doubles, so a 19-digit ID can change. NeatJSON's repair keeps every number exactly as written; see JSON big integer precision.
jq cannot help here: it is a strict parser and refuses the file before your filter runs. Remove the commas first, then pipe the clean JSON to jq.
How to stop trailing commas from coming back
- Generate JSON with a serializer.
JSON.stringify,json.dumpsandjson.Marshalnever write trailing commas. Building JSON by concatenating strings in a loop is the usual source of a final,. - Let the editor tell you. In VS Code, a
.jsonfile opened in JSON mode underlines a trailing comma as an error immediately. - Check in CI. Run
jq empty file.jsonorpython -m json.tool file.json > /dev/nullon changed JSON files; both exit with an error on a trailing comma. - Use the right format. If people edit the file by hand and the tool supports it, JSONC or JSON5 is friendlier. Otherwise keep strict JSON and validate it.
Frequently asked questions
Are trailing commas allowed in JSON?
No. The JSON grammar in RFC 8259 and ECMA-404 only allows a comma between two values, so a comma before a closing } or ] is a syntax error in every standard JSON parser. JSON5, JSONC and JavaScript object literals do allow them.
What error does a trailing comma cause?
In current Chrome and Node.js it is Expected double-quoted property name in JSON at position N for objects and Unexpected token ']' for arrays. Older versions say Unexpected token } in JSON at position N. Python 3.12 and earlier say Expecting property name enclosed in double quotes, and Python 3.13 and later say Illegal trailing comma before end of object.
How do I remove trailing commas from JSON?
Paste the JSON on this page and click Repair JSON. It removes every trailing comma with a parser, so commas inside strings are not changed, and lists the line and column of each fix. A regex such as ,\s*([}\]]) works on simple files but can corrupt strings that contain a comma followed by a bracket.
Why does Python say "Expecting property name enclosed in double quotes"?
After a comma, the parser expects another quoted key. If it finds } instead, there is a trailing comma. If it finds a single quote or a bare word, the keys are not double-quoted, which usually means the text came from str() of a Python dict instead of json.dumps().
Does VS Code allow trailing commas in JSON?
Not in plain JSON mode, where it marks them as errors. VS Code reads its own settings files as JSON with Comments, which tolerates comments and trailing commas there. You can set other files to the jsonc language mode with files.associations.
Can jq parse JSON with trailing commas?
No. jq is a strict parser and stops with an error such as Expected another key-value pair. Remove the trailing commas first, for example with Repair JSON on this page, then run jq on the result.
Is my JSON uploaded when I repair it?
No. Checking and repairing run in your browser and nothing is sent to a server.