JSON trailing comma error: how to fix it
“Expected double-quoted property name”, “Illegal trailing comma”, “Unexpected token ]” — these errors usually mean one thing: a comma after the last item in an object or array. Here is how to recognise the error in any language, find the comma in seconds, and stop it from coming back.
What the error means
A trailing comma is a comma after the last item in an object or array. JavaScript, Python and most modern languages allow it in their own code, so it slips into JSON easily — but the JSON standard (RFC 8259) does not allow it. A strict parser reads the comma, expects another key or value, and finds } or ] instead.
{
"name": "Asha",
"city": "Pune",
}
✓ Valid{
"name": "Asha",
"city": "Pune"
}
The fix is always the same: delete the comma after the last item. The rest of this guide shows how to find it quickly, what each language's error message looks like, and how to stop it from coming back.
The error message in each language
Different parsers describe the same mistake very differently, which is why this error is confusing. If you see any of these, look for a comma just before the position reported:
| Where | Error message |
|---|---|
Chrome, Edge, Node.js (JSON.parse) | Expected double-quoted property name in JSON at position 38 (line 4 column 1)In arrays: Unexpected token ']', "[1, 2, 3,]" is not valid JSON |
| Firefox | JSON.parse: expected double-quoted property name |
| Python 3.13 and later | Illegal trailing comma before end of object: line 3 column 17 |
| Python 3.12 and earlier | Expecting property name enclosed in double quotesIn arrays: Expecting value |
| Java (Jackson) | Unexpected character ('}' (code 125)): was expecting double-quote to start field name |
Go (encoding/json) | invalid character '}' looking for beginning of object key string |
.NET (System.Text.Json) | The JSON object contains a trailing comma at the end which is not supported in this mode. |
PHP (json_decode) | Returns null; json_last_error_msg() gives Syntax error |
Notice that several of these never mention a comma. “Expected double-quoted property name” means the parser saw a comma and was waiting for the next key — and the next character was }.
Find the trailing comma fast
- Paste the JSON into the Tagbrace validator and click Validate. It names the problem directly — “Trailing comma is not allowed in JSON” — and shows the line and column with a pointer.
- Go to the reported position. The error points at the closing
}or]; the stray comma is at the end of the line just above it. - Delete the comma and validate again. Large files often have more than one, so repeat until it passes.
In a large file you can also search for the patterns ,} and ,], allowing for spaces and line breaks between them. In editors that support regular expressions, search for ,\s*([\]}]) to find every one. Review each match before replacing: the same characters can appear legitimately inside a string value.
Trailing commas in arrays and nested objects
The same rule applies inside arrays:
✗ Invalid{
"tags": ["json", "api", "config",]
}
✓ Valid{
"tags": ["json", "api", "config"]
}
In nested objects the comma is often hidden in the middle of the file, at the end of an inner block, rather than at the very end:
✗ Invalid{
"server": {
"host": "localhost",
"port": 8080,
},
"debug": true
}
✓ Valid{
"server": {
"host": "localhost",
"port": 8080
},
"debug": true
}
Where trailing commas come from
- Deleting the last property by hand. You remove the final line of an object and forget the comma on the line that is now last.
- Copying from JavaScript or Python code. Code style guides often require trailing commas, so objects copied from source files bring them along.
- Config files that allow them. VS Code
settings.jsonandtsconfig.jsonare read as “JSON with comments” and tolerate trailing commas. Copy that content into a file read by a strict parser —package.json, an API request body, a JSON column in a database — and it fails. - Building JSON as a string in a loop. Appending
"item,"for every element leaves a comma after the last one.
Stop it at the source: generate JSON properly
If your code produces the JSON, never join the pieces by hand. This pattern always leaves a trailing comma:
✗ Invalid# Builds invalid JSON: a comma after every item, including the last
out = "["
for user in users:
out += '{"id": %d},' % user.id
out += "]"
Build normal lists and objects and let the JSON library write the commas:
✓ Validimport json
out = json.dumps([{"id": user.id} for user in users])
✓ Valid// Let the JSON library add the commas
const body = JSON.stringify(users.map((u) => ({ id: u.id })));
The same applies in every language: Jackson or Gson in Java, json_encode in PHP, encoding/json in Go and System.Text.Json in .NET never produce trailing commas.
When you can't edit the file: parse it leniently
Sometimes the JSON comes from a source you don't control. You can read it with a lenient parser, then write strict JSON back out.
JavaScript, with the json5 package:
import JSON5 from "json5";
const data = JSON5.parse(text); // accepts trailing commas and comments
const strict = JSON.stringify(data, null, 2); // valid JSON again
Python, with the json5 package:
import json, json5 # pip install json5
data = json5.loads(text)
strict = json.dumps(data, indent=2)
Some standard parsers also have an opt-in setting: JsonReadFeature.ALLOW_TRAILING_COMMA in Jackson and AllowTrailingCommas = true in .NET's JsonSerializerOptions. Use these only for input you read yourself. Anything you send to another system should be strict JSON, because you can't know whether its parser is lenient.
Avoid fixing trailing commas with a blind find-and-replace on production data. A regular expression can't tell a real trailing comma from the characters ,} inside a string such as "note": "use ,} here"; a parser can.
Related guides
- Common JSON errors and how to fix them
- JSON vs XML: key differences and when to use each
- How to convert SOAP XML to JSON
Check your own JSON or XML in seconds — free, and nothing is uploaded.
Open the formatter →