Trailing comma in JSON: How to Fix It in Python
A trailing comma is a comma after the last item in a JSON object or array. Python's json.loads() rejects it. Here is how to spot it and fix it.
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →A trailing comma in JSON is a comma placed after the last member of an object or the last element of an array, and the JSON standard does not allow it. Python's json.loads() raises a JSONDecodeError, so you remove the comma or repair the text before parsing.
What “Trailing comma in JSON” means
The parser found a comma and then a closing brace or bracket, with no value between them. The JSON grammar only allows a comma when another member or element follows it. Python's json module has no message with this exact wording. For an object it reports Expecting property name enclosed in double quotes. For an array it reports Expecting value. Both point at the position right after the comma.
Causes and the fix for each
A comma was left after the last object member.
This happens when you delete or reorder lines and forget the comma on the line before the closing brace.
Before (fails):{"a": 1, "b": 2,}
After (valid):
{"a": 1, "b": 2}
A comma was left after the last array element.
Python lists allow this, so people carry the habit over to JSON.
Before (fails):{"ids": [1, 2, 3,]}
After (valid):
{"ids": [1, 2, 3]}
A Python dict or list was written out with str() instead of json.dumps().
Pretty-printers and hand-built strings can leave a comma at the end of a block, and str() also produces single quotes and True/None.
Before (fails):{"items": [{"id": 1,},]}
After (valid):
{"items": [{"id": 1}]}
A JSON file was edited by hand after a line was removed.
Config files are easy to break this way because the last line moves when you delete an entry.
Before (fails):{
"debug": true,
"port": 8080,
}
After (valid):
{
"debug": true,
"port": 8080
}
An LLM or a code generator copied a JavaScript or JSON5 style.
JavaScript object literals and JSON5 accept trailing commas. Model output often follows that style, but strict JSON parsers do not.
Before (fails):{"tags": ["a", "b",], "ok": true,}
After (valid):
{"tags": ["a", "b"], "ok": true}
How to fix it in Python
- Read the line and column in the error message. The comma is usually just before that position, on the previous token.
- Delete the comma that sits directly before a closing } or ].
- Paste the text into the Validator to see every syntax problem at once, since a file with one trailing comma often has more.
- If the text came from a model or a tool you do not control, run it through the Repair tool, which removes trailing commas and fixes related damage.
- In your own code, build JSON with json.dumps() on a dict or list instead of joining strings by hand. It does not emit trailing commas.
- If you must accept untrusted input, wrap json.loads() in try/except and run a cleanup pass only after the first parse fails.
import json
import re
def load_json(text):
try:
return json.loads(text)
except json.JSONDecodeError as first_error:
# Remove a comma that sits right before } or ].
# This regex can also change commas inside string values,
# so it runs only after a normal parse has failed.
cleaned = re.sub(r",\s*([}\]])", r"\1", text)
try:
return json.loads(cleaned)
except json.JSONDecodeError:
raise first_error
data = load_json('{"a": 1, "b": [1, 2,],}')
print(data)
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Expecting property name enclosed in double quotes | Comma after the last member of an object | Delete the comma before the closing brace |
| Expecting value | Comma after the last element of an array | Delete the comma before the closing bracket |
| Expecting value: line 1 column 2 (char 1) | Array starts with a comma, such as [,1] | Remove the leading comma |
| Expecting property name enclosed in double quotes: line 1 column 2 (char 1) | Object starts with a comma, such as {,"a":1} | Remove the leading comma |
| Expecting value after a double comma | Two commas in a row, such as [1,,2] | Delete the extra comma |
Related errors
- Expecting property name enclosed in double quotes (Python Fix)
- JSONDecodeError: Expecting ',' delimiter: How to Fix It in Python
- Fix Python JSON Errors: json.loads() Decode Errors and How to Repair Them
- Single quotes are not valid JSON: Fixing json.loads() in Python
- JSONDecodeError: Extra data: How to Fix It in Python
Key takeaways
- A comma before } or ] is invalid JSON under RFC 8259.
- Python reports it as Expecting property name enclosed in double quotes for objects and Expecting value for arrays.
- The comma sits just before the position named in the error.
- json.dumps() does not produce trailing commas, so build JSON with it.
- For input you do not control, parse first and clean up only on failure, or use the Repair tool.
FAQ
Does JSON allow a trailing comma?
No. The JSON grammar in RFC 8259 allows a comma only between two members or two elements. A comma before a closing brace or bracket is a syntax error.
Why does Python allow trailing commas in lists but not in JSON?
Python syntax and JSON syntax are separate grammars. Python permits a trailing comma in lists, tuples and dicts. JSON is a data format with its own stricter rules, and json.loads() follows those rules.
Which error message does Python show for a trailing comma?
For an object, json.loads() raises JSONDecodeError with Expecting property name enclosed in double quotes. For an array, it raises JSONDecodeError with Expecting value. The line and column point just after the comma.
Can I make json.loads() accept trailing commas?
Not with an option. The standard json module has no setting for it. You can clean the text first, or use a third-party parser that supports JSON5 or a relaxed mode.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles