Common causes
1. The last property was deleted or moved
Removing the final line of an object leaves the comma of the line above dangling. This is the most common source, especially in package.json and other hand-edited config.
{
"name": "pastekit",
"private": true,
}{
"name": "pastekit",
"private": true
}2. A trailing comma at the end of an array
Lists that grow one line at a time often end with a comma. Chrome reports it as an unexpected ], Python 3.12 and earlier as “Expecting value”. The wording differs from the object case because after a comma an array expects any value, not specifically a key.
{"allowedOrigins": ["https://example.com", "https://admin.example.com",]}{"allowedOrigins": ["https://example.com", "https://admin.example.com"]}3. Copied from JavaScript or a JSONC file
Code formatters such as Prettier add trailing commas to JavaScript objects, and JSONC files such as tsconfig.json and VS Code settings allow them. Pasting that text into a strict JSON parser fails. Use a JSONC or JSON5 parser for those files, or remove the commas.
{
"compilerOptions": {
"strict": true,
"target": "ES2022",
},
}{
"compilerOptions": {
"strict": true,
"target": "ES2022"
}
}4. JSON built by a loop that appends ", " after every item
Templates and string-building loops that write a comma after each element leave one after the last. Let a serialiser produce the text instead, which also takes care of quoting and escaping.
items_json = "[" + "".join(f'"{sku}", ' for sku in skus) + "]"items_json = json.dumps(skus)How each parser reports it
Almost every parser points at the closing bracket, and most never mention a comma at all. For {"a":1,} and [1,2,3,]:
- Node 20 and later, current Chrome:
Expected double-quoted property name in JSON at position 7for the object,Unexpected token ']', "[1,2,3,]" is not valid JSONfor the array. Node 22 adds the line and column. - Node 18 and earlier:
Unexpected token } in JSON at position 7andUnexpected token ] in JSON at position 7. - Python up to 3.12:
Expecting property name enclosed in double quotesandExpecting value. - Python 3.13 and simplejson:
Illegal trailing comma before end of object, and the array equivalent, with the position on the comma itself. - Go
encoding/json:invalid character '}' looking for beginning of object key stringandinvalid character ']' looking for beginning of value. - Jackson (Java):
Unexpected character ('}' (code 125)): was expecting double-quote to start field name. - System.Text.Json (.NET):
The JSON object contains a trailing comma at the end which is not supported in this mode.This one does name the comma.
Newtonsoft.Json accepts trailing commas silently, so a file can load in one .NET service and be rejected by a neighbouring one using System.Text.Json.
Removing them safely, or accepting them on purpose
Avoid a blanket regular expression such as ,\s*}. It cannot tell structure from data, so it also edits a string value that happens to contain ,}. PasteKit’s Fix it button and the JSON Repair page read the document with a tolerant parser, remove only real trailing commas and show you the changes first.
If people edit the file by hand and you want to tolerate commas, turn the option on explicitly in the reader rather than rewriting the text:
- System.Text.Json:
AllowTrailingCommas = trueonJsonDocumentOptionsorJsonSerializerOptions. - Jackson:
JsonReadFeature.ALLOW_TRAILING_COMMAon aJsonMapperbuilder. - Microsoft’s jsonc-parser: the
allowTrailingCommaparse option. - Python: the standard
jsonmodule has no such switch, so use a JSON5 library for those files.
Keep this for human-written configuration. Data exchanged between programs should stay strict, because you cannot know which parser reads it next.
Catching it before it reaches production
A one-line check in CI is enough for most repositories:
# Exits non-zero and prints the line and column if the file is invalid
python -m json.tool package.json > /dev/null
jq empty file.json does the same job and exits non-zero on invalid input. The pre-commit project ships a check-json hook for checking every staged file, and eslint-plugin-jsonc has a jsonc/comma-dangle rule if you already run ESLint.
Do not rely on Prettier as a validator here. Its JSON parser is lenient: prettier --write removes the comma and carries on, and prettier --check only reports the file as unformatted. That is useful as a fixer, but it will not tell you a file was invalid.
If a commit keeps reintroducing the comma, look at why it exists. People add trailing commas in code to keep diffs to one line per change. Formats that allow them, such as JSONC or YAML, may suit that file better than JSON does.
Frequently asked questions
Why does JavaScript accept trailing commas but JSON does not?
JSON’s grammar was fixed in the early 2000s, and JavaScript only standardised trailing commas in object literals with ES5 in 2009. RFC 8259 still defines an object as members separated by commas, with nothing allowed after the last one.
Which formats allow trailing commas?
JSON5 allows them, and YAML flow collections do too. JSONC is looser: tsconfig.json and VS Code settings accept them, but not every JSONC parser does by default. Plain JSON, JSON Lines and package.json do not.
What does Python 3.13 say about trailing commas?
Python 3.13 added a specific message, “Illegal trailing comma before end of object” (or “of array”), pointing at the comma itself. Earlier versions report “Expecting property name enclosed in double quotes” or “Expecting value”.
Why does the error point at the line after the comma?
The comma is legal on its own, because it could be followed by another item. The parser only knows something is wrong when it reaches the closing bracket, so that is the position it reports. Look at the last token before that bracket.
Can PasteKit remove trailing commas for me?
Yes. Click Fix it: it removes every trailing comma, along with other common problems, and shows the changes as a diff before you accept them.