Tutorial · 5 min read
How to Fix JSON Trailing Commas and Single Quotes
Trailing commas and single quotes are the #1 reason JSON parsers fail. An auto-fix trailing commas with JSON Repair tool catches and fixes both instantly — no manual scanning required.
1. Trailing Commas: The Most Common JSON Mistake
A trailing comma is a comma after the last element in an object or array. It's valid in JavaScript and many other languages, but the JSON specification (RFC 7159) explicitly forbids it:
// ❌ Invalid JSON — trailing comma after "age"
{
"name": "Alice",
"age": 30, // ← this comma breaks the parser
}
// ❌ Also invalid — trailing comma in array
["red", "green", "blue",]
// ✅ Valid JSON
{
"name": "Alice",
"age": 30
}
// ✅ Valid array
["red", "green", "blue"]
The trailing comma is a classic copy-paste artifact. You add a new field, and the comma you inserted at the end of the previous line accidentally remains after the last item. The parser refuses to accept it.
To spot trailing commas manually, look for commas on the line just before a closing brace } or bracket ]. Syntax-highlighting editors often flag them in red.
2. Single Quotes: Where They Creep In
JSON strings must use double quotes ("). Single quotes (') are not valid, even though they work in JavaScript, Python, and many other languages:
// ❌ Invalid JSON — single quotes
{ 'name': 'Alice', 'role': 'Engineer' }
// ❌ Also invalid — mixed quotes
{ "name": 'Alice' }
// ✅ Valid JSON
{ "name": "Alice", "role": "Engineer" }
Single quotes commonly appear when developers hand-write JSON (because typing Shift+' is easier than Shift+"), or when JSON is copied from Python repr() output or JavaScript template literals that mix quotes.
Key names also require double quotes. { name: "Alice" } is invalid — it must be { "name": "Alice" }.
3. Other Syntax Errors a Repair Tool Handles
Beyond trailing commas and single quotes, a good JSON repair tool can fix several other common issues:
| Error | Example | Repair Action |
|---|---|---|
| Missing quotes on keys | {name: "Alice"} |
Add double quotes |
| Extra commas in objects | {,,"a":1} |
Remove extra commas |
| Missing closing bracket | {"a":[1,2 |
Append ]} |
| Unescaped control chars | "line1\nline2" |
Escape properly |
The JSON Repair tool handles all these cases automatically, outputting valid JSON ready for any parser.
4. Manual Fix vs Automated Repair
You have two approaches to fix these errors:
- Manual fix (search and replace):
- Find trailing commas: search
,\s*[}\]]using regex and remove them - Find single quotes: replace
'value'with"value"— but be careful not to replace apostrophes inside strings - Add missing quotes to keys: wrap unquoted key names in double quotes
- Find trailing commas: search
- Automated repair: Paste the broken JSON into the JSON Repair tool. It fixes all these issues in one click, handles edge cases (like apostrophes inside single-quoted strings), and outputs valid JSON.
// Regex to find trailing commas in VS Code: // Search: ,(\s*[}\]]) // Replace: $1 // (Use regex mode)
For one-off fixes, manual search-and-replace is fine. For repeated work — especially from an API that intermittently sends malformed JSON — automated repair saves hours.
5. Always Validate After Repair
Even the best repair tool can make mistakes with ambiguous input. Always validate the output:
- Run the broken JSON through the repair tool
- Paste the repaired output into a JSON validator
- Confirm the validator says "Valid JSON" before deploying or using the data
// After repair — should validate successfully
{
"name": "Alice",
"greeting": "It's a nice day",
"items": ["a", "b", "c"]
}
Note: the repair tool correctly handles apostrophes inside values (It's) — it knows that the single quote in It's is not a string delimiter.
For an extra layer of safety, keep the JSON Editor open in a second tab — it validates in real-time as you edit, so you can spot any remaining issues immediately.
Try the JSON Repair Tool
Paste broken JSON — trailing commas, single quotes, missing brackets — and get clean, valid JSON instantly. 100% browser-based, no data sent to servers.
Fix JSON Now →Best Practices to Avoid Trailing Commas and Single Quotes
- Use a linter. Configure your editor to flag trailing commas in JSON files. ESLint with
jsoncplugin catches these automatically. - Always use double quotes. Train yourself to type
"key": "value"instead of'key': 'value'. It becomes automatic after a week. - Use a JSON generator library. Never hand-write large JSON files. Use
JSON.stringify()in JavaScript,json.dumps()in Python, orserde_json::to_string()in Rust. - Validate before commit. Add a pre-commit hook that runs your JSON through a validator and rejects malformed files.
- Keep a backup of the original. Before repairing, always save the original broken JSON. The repair might not be perfect for edge cases.
Frequently Asked Questions
Why does JavaScript allow trailing commas but JSON doesn't?
JavaScript's object literal syntax evolved to allow trailing commas for convenience — it makes adding new items cleaner in diffs. JSON was designed as a strict, language-independent data interchange format (RFC 7159) and deliberately excludes trailing commas for maximum portability between parsers.
Does JSON support single quotes for strings?
No. The JSON specification (RFC 7159) requires strings to be enclosed in double quotes. Single-quoted strings are valid in JavaScript but not in strict JSON. Most JSON parsers will reject single-quoted strings as syntax errors.
Will a JSON repair tool remove comments from my file?
Most repair tools strip non-standard content like comments (// or /* */) because they are not valid JSON. If you need to preserve comments, use a JSON5-compatible parser instead, or keep a backup of the original file before repairing.
Can I batch-repair multiple JSON files at once?
Some online tools support batch upload and repair. For local files, you can use a command-line JSON repair module like 'jsonrepair' (npm) in a bash loop to process directories.
Is it safe to paste sensitive JSON into an online repair tool?
Yes — as long as the tool processes everything in the browser. Client-side tools never send your data to a server. Always check for a claim like '100% client-side' or 'your data never leaves your browser' before pasting sensitive information.
Looking for more guides? See the full JSONXX How To index.