Trailing comma in JSON: How to Fix It in JavaScript
JSON.parse() throws a SyntaxError when a comma sits before a closing brace or bracket. See why it happens in JavaScript and how to 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 item of an object or array, and the JSON standard does not allow it. JSON.parse() in JavaScript throws a SyntaxError when it reads one.
What “Trailing comma in JSON” means
JSON.parse() rejected your text because a comma is followed by a closing brace or bracket instead of another value. The JSON grammar only allows a comma between two members or elements. JavaScript source code is more relaxed, so a literal with a trailing comma runs fine in your editor. The same text fails once it is parsed as JSON.
Causes and the fix for each
A comma was left after the last property of an object.
JavaScript object literals accept a comma after the final property, and many formatters add one for cleaner diffs. JSON does not. When you copy an object literal into a JSON file or request body, the extra comma stays and the parser rejects it with a SyntaxError.
Before (fails):{"id": 1, "ok": true,}
After (valid):
{"id": 1, "ok": true}
A comma was left after the last element of an array.
Arrays in JavaScript source also tolerate a final comma. A JSON array does not. This often appears after you delete the last item of a list by hand and leave the separator from the previous line behind.
Before (fails):{"tags": ["a", "b",]}
After (valid):
{"tags": ["a", "b"]}
The last item was deleted or commented out and its neighbor kept the comma.
Removing the final entry from a config file leaves the comma on the line above it. Comment syntax does not exist in JSON either, so a commented-out line is usually deleted by hand, and the comma is easy to miss.
Before (fails):{"a": 1, "b": 2,
}
After (valid):
{"a": 1, "b": 2
}
A nested object or array ends with a comma.
The error can sit deep inside a large document. A comma after the last member of an inner object breaks the whole parse, even when the outer structure looks correct. The error position points at the closing brace, not at the start of the document.
Before (fails):{"user": {"name": "Ada",}, "active": true}
After (valid):
{"user": {"name": "Ada"}, "active": true}
Text was built by hand with a loop that adds a comma after every item.
Code that concatenates strings and appends a comma after each element leaves one comma at the end. Building the data as a JavaScript object and calling JSON.stringify() avoids the problem, because it writes separators only between items.
Before (fails):{"items": [1,2,3,]}
After (valid):
{"items": [1,2,3]}
How to fix it in JavaScript
- Read the error position. V8 names the unexpected token and often the character position, which points at the closing brace or bracket right after the extra comma.
- Open the text at that position and delete the comma that comes just before the closing } or ].
- Paste the text into the Validator to check the rest of the document. The validator reports the next problem if there is one.
- If the file is large or comes from a tool you do not control, run it through the Repair tool. It removes trailing commas and returns valid JSON.
- Stop writing JSON by string concatenation. Build a JavaScript object and call JSON.stringify() so separators are placed correctly.
- Use the Formatter on the fixed result to confirm the structure reads the way you expect.
function parseLenient(text) {
try {
return { ok: true, value: JSON.parse(text) };
} catch (err) {
// Remove commas that sit right before } or ].
// This regex can also change text inside strings, so log and review the result.
const cleaned = text.replace(/,\s*([}\]])/g, '$1');
try {
return { ok: true, value: JSON.parse(cleaned), repaired: true };
} catch (err2) {
return { ok: false, error: err2.message };
}
}
}
console.log(parseLenient('{"a": 1, "b": [1, 2,],}'));
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Unexpected token } in JSON at position 20 | Comma before a closing brace in older V8 versions | Delete the comma before the } |
| Unexpected token ] in JSON at position 12 | Comma before a closing bracket in older V8 versions | Delete the comma before the ] |
| Expected double-quoted property name in JSON at position 20 | Newer V8 wording for a comma before } in an object | Delete the comma before the } |
| JSON.parse: expected double-quoted property name | Firefox wording for a trailing comma in an object | Delete the comma before the } |
| JSON Parse error: Unexpected token '}' | Safari wording for a trailing comma in an object | Delete the comma before the } |
Related errors
- JavaScript JSON.parse() Errors: What They Mean and How to Fix Them
- Invalid JSON Meaning: What It Is and How to Fix It
- JSON Missing Bracket Error: How to Find and Fix It
- [Fix JSON Single Quote Errors Instantly — Free Online Tool [2026]](/blog/fix-json-single-quotes)
- Invalid JSON: JavaScript JSON.parse() Fails on NaN and Infinity
Key takeaways
- A comma before } or ] is invalid in JSON.
- JavaScript literals allow trailing commas, JSON does not.
- The error position usually points at the closing brace or bracket.
- Build output with JSON.stringify() instead of joining strings.
- Use the Repair tool to clean up files you cannot easily edit.
FAQ
Does JSON allow a trailing comma?
No. The JSON grammar allows a comma only between two values or members. A comma before a closing brace or bracket makes the text invalid.
Why does a trailing comma work in my JavaScript code but fail in JSON.parse()?
JavaScript source syntax permits trailing commas in object and array literals. JSON is a separate, stricter format. JSON.parse() follows the JSON grammar, not the JavaScript one.
Can I strip trailing commas with a regular expression?
Yes, for simple cases. A pattern that removes a comma before } or ] works on plain data. It can also alter text inside string values that contain such a sequence, so check the output.
Why does the error message differ between browsers and Node.js?
The message text is not defined by the JSON standard. Each engine writes its own SyntaxError wording, so Chrome, Firefox and Safari report the same problem with different messages.
Do JSON5 or JSONC allow trailing commas?
Yes. JSON5 allows them by design, and JSONC parsers commonly accept them. Standard JSON.parse() does not read either format.
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