Clean JSON copied from ChatGPT, Claude, Cursor, Codex, and similar tools.
AI tools like ChatGPT, Claude, Gemini, Cursor, and GitHub Copilot are useful for generating JSON examples, data schemas, configuration files, and API mock responses. But when you copy JSON from an AI response, the output is often not valid JSON — it contains Markdown formatting, explanatory prose, trailing commas, JavaScript-style syntax, or comments. This guide covers every common problem with AI-generated JSON, how to clean it up, and how to prevent these issues before they reach your codebase.
Large language models are trained on human-readable text — including millions of code tutorials, blog posts, and documentation pages that mix prose with code. When a model generates a JSON example, it is producing text that looks like what appears in tutorials, not necessarily text that passes a strict parser. Several factors make AI JSON output unreliable:
```json ... ```) because that is the convention in chat interfaces. The fence characters are not JSON.The most common issue. The AI wraps the JSON in triple backticks, which are Markdown syntax, not JSON.
AI output:
```json
{
"name": "Report",
"enabled": true
}
```
Valid JSON (fences removed):
{
"name": "Report",
"enabled": true
}
Copy only the content between the fence markers. Do not include the ```json opening or the closing ```.
AI chat tools explain their output. This is useful for understanding but must be removed before parsing.
AI output:
Here is the user object you requested:
{
"id": 42,
"name": "Ava"
}
Let me know if you need additional fields.
Valid JSON:
{
"id": 42,
"name": "Ava"
}
Copy from the opening { or [ to the matching closing character. Everything outside that range is prose.
Very common in AI output. Models trained on JavaScript examples often produce trailing commas after the last property in objects and arrays, which is valid JavaScript but invalid JSON.
Invalid:
{
"name": "Report",
"enabled": true,
"fields": ["id", "status",],
}
Valid:
{
"name": "Report",
"enabled": true,
"fields": ["id", "status"]
}
Use Auto-fix in the JSON Formatter to remove all trailing commas at once rather than finding them manually in large outputs.
AI tools sometimes generate JSON using JavaScript string syntax. JSON requires double quotes for all keys and string values.
Invalid:
{'name': 'Ava', 'role': 'admin'}
Valid:
{"name": "Ava", "role": "admin"}
JavaScript object literals allow keys without quotes. JSON requires every key to be a double-quoted string.
Invalid:
{name: "Ava", active: true, score: 42}
Valid:
{"name": "Ava", "active": true, "score": 42}
AI models frequently add inline comments to explain fields. JSON does not support comments of any kind.
Invalid:
{
"name": "Ava", // display name
"role": "admin", /* access level */
"score": 95 // out of 100
}
Valid:
{
"name": "Ava",
"role": "admin",
"score": 95
}
Auto-fix removes // and /* */ comments automatically.
AI tools sometimes use ... as a placeholder to indicate that more items would appear in a real response. This is not valid JSON.
Invalid:
{
"users": [
{"id": 1, "name": "Ava"},
{"id": 2, "name": "Ben"},
...
]
}
Valid (placeholder removed):
{
"users": [
{"id": 1, "name": "Ava"},
{"id": 2, "name": "Ben"}
]
}
Auto-fix cannot safely remove ellipsis placeholders because it does not know what value to substitute. Remove them manually.
AI models occasionally drop quotes on string values, especially for short strings that look like identifiers.
Invalid:
{"status": ok, "type": active}
Valid:
{"status": "ok", "type": "active"}
```json ... ```, remove those lines. Copy only the JSON content.{ or [ and after the closing } or ].The fastest cleanup is no cleanup. Prompting the AI tool more precisely reduces — but does not eliminate — invalid output. Here are prompts that produce cleaner results:
Basic prompt for valid JSON:
Return only valid JSON. No Markdown, no comments, no trailing commas, no explanations.
For a specific schema:
Return a valid JSON object with these fields: id (integer), name (string), active (boolean), roles (array of strings). No explanations, no Markdown fences, no comments.
For a JSON array:
Return a JSON array of 5 user objects. Each object must have: id, name, email. Output only valid JSON — no prose, no backtick fences, no trailing commas.
Even with careful prompting, validate the output before using it. AI models do not guarantee syntactic correctness — they predict likely token sequences, and a highly probable next token is not always a valid JSON token.
If you are using an AI tool to generate a mock API endpoint (for example, with a service like Mocky or a local server), you can validate and inspect the live response directly in the formatter using Load from URL. Enter the endpoint URL, and the formatter fetches the response and loads it into the editor for formatting and validation — no copy-pasting required.
No. Auto-fix applies deterministic rules: it removes trailing commas, converts single quotes to double quotes, quotes unquoted keys, and strips comments. These transformations follow the JSON specification exactly — there is no language model involved. This means Auto-fix is fast, predictable, and works offline entirely in your browser.
Language models generate the most statistically likely next token, not the most syntactically correct one. A trailing comma is extremely common in code on the internet (especially JavaScript), so models produce it frequently even when prompted for JSON. Always validate AI output before using it. The formatter's Auto-fix will remove trailing commas reliably.
Validate all AI-generated schemas carefully. While AI tools produce structurally plausible schemas, they may get field names wrong, omit required fields, use incorrect types, or misunderstand your actual data model. Use JSON Schema validation tools (or the formatter's structure view) to verify the schema against real data before deploying it.
null for some values. Is that valid?Yes. null is a valid JSON value type alongside string, number, boolean, object, and array. {"value": null} is perfectly valid JSON. The issue only arises if the AI writes undefined or omits a value entirely without the proper null keyword.
Paste the entire output into the formatter and click Format. The validator checks every value, key, and structural element in one pass — it does not just check the first few lines. If the JSON is valid, formatting succeeds and you see the properly indented structure. If it is invalid, the editor jumps to the first error location.
Try what you just learned — paste your JSON into the formatter and validate it instantly. Free, browser-only, no data sent anywhere.
Need to clean JSON from an AI response? Paste it into the formatter and validate it locally.
Format AI JSON Output