If you've ever asked ChatGPT to return JSON, you've probably seen this error:
SyntaxError: Unexpected token
ChatGPT and GPT-4o are great at generating almost valid JSON — close enough for a human to read, but broken enough to crash JSON.parse(). After repairing thousands of broken JSON documents, here are the 5 most common ways ChatGPT breaks your JSON, with fixes for each.
1. Markdown Code Fence Wrapping
The problem: You ask for JSON. ChatGPT wraps it in markdown:
Here is the JSON you requested:
\`\`\`json
{"name": "Alice", "role": "engineer"}
\`\`\`
The fix: Strip everything before the first { and after the last }:
const raw = chatgptResponse;
const start = raw.indexOf('{');
const end = raw.lastIndexOf('}');
const clean = raw.slice(start, end + 1);
JSON.parse(clean);
2. Trailing Commas
The problem: ChatGPT adds trailing commas after the last array/object item:
{"items": ["apple", "banana", "cherry",]}
This is valid JavaScript but illegal in strict JSON.
The fix:
cleaned = json.replace(/,(\s*[}\]])/g, '$1');
3. Python Booleans
The problem: ChatGPT sometimes outputs Python-style booleans:
{"active": True, "deleted": False, "metadata": None}
The fix:
cleaned = cleaned
.replace(/\bTrue\b/g, 'true')
.replace(/\bFalse\b/g, 'false')
.replace(/\bNone\b/g, 'null');
4. JavaScript Comments
The problem: ChatGPT adds helpful comments that break JSON:
{
// User profile
"id": 42,
"name": "Alice" /* primary user */
}
The fix: Strip both line and block comments (careful not to touch strings):
// Simple version (doesn't handle comments inside strings)
cleaned = cleaned
.replace(/\/\/.*$/gm, '')
.replace(/\/\*[\s\S]*?\*\//g, '');
5. Truncated Responses
The problem: When ChatGPT hits its token limit, JSON gets cut off mid-stream:
{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "na
The fix: This one is hard to fix with regex. You need to close unclosed strings, arrays, and objects. A robust approach:
// Close unclosed structures
let depth = { brace: 0, bracket: 0, inString: false };
// ... (requires a state machine parser)
The Easy Way: Use a JSON Fixer
Writing all these cleanup rules yourself is tedious and error-prone. I built AI JSONMedic — a free online tool that handles all 5 of these cases (and more) in one click.
Just paste the raw ChatGPT output — markdown wrappers, Python booleans, comments, and all — and get clean, valid JSON back instantly. It runs entirely in your browser, so your data never leaves your device.
Try it free: aijsonmedic.com
TL;DR
| Error | Cause | Quick Fix |
|---|---|---|
| Markdown wrapping | ChatGPT adds code fences | Strip before first {
|
| Trailing commas | JS habit, illegal in JSON | Regex remove |
| Python booleans | True/False/None | Replace with true/false/null |
| Comments | Helpful but illegal | Strip // and /* */ |
| Truncation | Token limit hit | Close unclosed structures |
Have you run into other ChatGPT JSON bugs? Drop them in the comments!
Top comments (0)