How to Fix Invalid JSON: Repair Broken JSON Online and in Code
Your JSON won't parse. Maybe it came from ChatGPT, a log file, a Python print(), or an API response that was cut off. You don't want to find every missing quote by hand. This guide shows the fastest way to fix invalid JSON: find the error, repair it automatically, and check that nothing was lost. It also shows how to do the same in JavaScript and Python code.
The 30-second fix
- Paste the text into JSON Repair. It fixes the common problems at once and lists every change it made.
- Read the list of fixes. If it says "Added missing closing '}'", the data was probably cut off, so check that nothing important is missing.
- Paste the result into the JSON Validator to confirm it is valid, or open it in the JSON Viewer to explore it.
A real example
This is the kind of text AI chatbots and quick scripts produce. It has eight different problems:
```json
{
name: 'Ann Lee', // customer
"active": True,
"tags": ["vip", "new",],
"note": “call back”,
"address": { "city": "Oslo"After repair:
{
"name": "Ann Lee",
"active": true,
"tags": ["vip", "new"],
"note": "call back",
"address": { "city": "Oslo" }
}And the list of fixes JSON Repair reports:
| Problem | Fix |
|---|---|
```json at the start | Removed Markdown code fence |
name without quotes | Added quotes around property names |
'Ann Lee' in single quotes | Converted single quotes to double quotes |
// customer | Removed comments |
True (Python) | Converted to true |
"new",] trailing comma | Removed trailing commas |
“call back” curly quotes | Replaced with straight quotes |
No closing } } | Added missing closing '}' |
Where broken JSON comes from (and the quick fix for each)
1. ChatGPT and other AI tools
AI answers often wrap JSON in ```json fences, add a sentence before it ("Here is the JSON:"), leave trailing commas, or stop in the middle when the answer is too long. JSON Repair removes the fence and the text before it and closes open brackets.
2. Python print() output
{'id': 7, 'active': True, 'parent': None} ← printed Python dict, not JSON
{"id": 7, "active": true, "parent": null} ← JSONPython prints dicts with single quotes and True/False/None. The real fix is in the code: use print(json.dumps(data)) instead of print(data). For text you already have, JSON Repair converts it. See the Python JSON guide.
3. JavaScript objects
const user = { name: 'Ann', tags: ['a', 'b'], }; ← JavaScript, not JSONUnquoted keys, single quotes and trailing commas are fine in JavaScript but not in JSON. JSON Repair removes const user = and the ; and fixes the rest. In code, use JSON.stringify(user) to produce real JSON.
4. Config files with comments
Files like tsconfig.json and VS Code settings allow // comments, but standard JSON does not. See Can JSON have comments? for the details. JSON Repair strips //, /* */ and # comments.
5. Cut-off API responses and logs
A response that stops at a size limit, or a log line that was truncated, ends in the middle of a value: {"items": [{"id": 1}, {"id": 2. Repair closes it so you can read what is there, but the rest of the data is gone. Fetch it again if you need all of it.
6. Text copied from Word, Slack or email
These apps replace straight quotes " with curly quotes “ ”, which no JSON parser accepts. JSON Repair replaces them. To avoid this, share JSON in a code block or as a file.
Fix invalid JSON in code
If you get broken JSON regularly (for example from an AI API), repair it in code before parsing.
JavaScript: jsonrepair
npm install jsonrepairimport { jsonrepair } from 'jsonrepair'
const broken = "{name: 'Ann', active: True, tags: ['vip',],}"
const fixed = jsonrepair(broken)
// '{"name": "Ann", "active": true, "tags": ["vip"]}'
const data = JSON.parse(fixed)Python: json-repair
pip install json-repairfrom json_repair import repair_json, loads
broken = "{name: 'Ann', active: True, tags: ['vip',],}"
print(repair_json(broken))
# {"name": "Ann", "active": true, "tags": ["vip"]}
data = loads(broken) # repair and parse in one step
print(data["tags"]) # ['vip']function parseLenient(text) {
try {
return JSON.parse(text)
} catch {
console.warn('Invalid JSON received, repairing it')
return JSON.parse(jsonrepair(text))
}
}When you should not repair automatically
- Payments, orders and other important data: reject invalid input and ask the sender to fix it. A guess is not good enough.
- Cut-off data: repair makes it valid but not complete. Missing items stay missing.
- Duplicate keys:
{"a": 1, "a": 2}is technically allowed, but most parsers keep only the last value. JSON Repair warns you about this, so check which value is correct.
Find the error by hand
If you prefer to fix it yourself, the JSON Validator shows the exact line and column of the first error. These guides explain the messages:
- Unexpected token in JSON: what
Unexpected token < in JSON at position 0and similar errors mean - 9 common JSON syntax errors: wrong and correct examples side by side
Summary
| Problem | Best fix |
|---|---|
| One broken file or snippet | Paste into JSON Repair, check the list of fixes |
| AI output in your app | Use JSON mode / structured output; repair as a fallback |
| Python dict text | Use json.dumps() in the code |
| JavaScript object text | Use JSON.stringify() in the code |
| Cut-off response | Fetch it again; repair only to read what is there |