How to fix invalid JSON from an LLM
By Flavio
Fix invalid JSON from an LLM in the browser. Repair code fences, trailing commas, comments, single quotes, and cut-off brackets, then copy the valid JSON.
Free toolThis tutorial uses the JSON Repair. It runs in your browser, so open it in another tab and follow along.
To fix invalid JSON from an LLM, paste the model reply into a repairer that unwraps the code fence, drops the comments and trailing commas, and prints JSON that JSON.parse accepts. Then read the list of fixes before you trust the object.
We’ll use the JSON repair tool. It runs in the browser. The reply is not uploaded, and the only thing that can land in the URL is ?indent=0 when you minify.
Why the reply fails to parse
JSON.parse wants one JSON value. A model often adds a sentence, a markdown fence, a comment, or a Python False. Any one of those makes the parse throw, even when the object inside is right.
The benchmarks post shows a case like that. The model returned the right object, wrapped in a markdown code fence, and JSON.parse on the whole string threw.
package.json has the same strictness. Comments and trailing commas are not valid JSON, which the package.json guide spells out. Parsing JSON with Node.js is the longer look at JSON.parse itself.
Repair the sample
- Open JSON repair.
- Click Load sample, or paste your own reply into Broken JSON.
- Read What changed.
- Copy Valid JSON, or download
repaired.json.
The sample is a fenced object with two trailing commas, a Python False, a single-quoted string, and a // comment:
```json
{
"title": "How to count tokens",
"tags": ["ai", "javascript",],
"draft": False,
"notes": 'check the tokenizer',
// the model added a comment
}
```
One trailing comma sits after "javascript" in the tags array. The other sits after the notes string, before the comment and the closing brace.
The page reports these fixes, in this order:
- Removed a markdown code fence.
- Removed 1 comment.
- Removed 2 trailing commas.
- Converted 1 single-quoted string to double quotes.
- Converted the Python literal False to JSON.
The valid JSON is:
{
"title": "How to count tokens",
"tags": [
"ai",
"javascript"
],
"draft": false,
"notes": "check the tokenizer"
}
Tick Minify output and the same value is written with no extra space. That choice is the indent=0 query in the URL. The JSON itself stays on the page. Files up to 2 MB open from Open file.
What the tool repairs
If the text is already valid JSON, the page pretty-prints it and the note says “This JSON is already valid.” A repeated key in valid JSON keeps the later value, because that is what JSON.parse does, and no repair is listed.
When the text is not valid, the page only does the repairs it can name:
| Mistake in the reply | What you get |
|---|---|
| Curly quotes | Straight quotes |
A json, javascript, or js fence around an object or array | The value inside, plus a note if text around the fence was dropped |
Prose before the first { or [, with no fence | That prose dropped |
// and /* */ comments | Removed |
| Trailing commas | Removed |
| Single-quoted strings | Double-quoted JSON strings |
| Unquoted keys | Quoted keys |
Python True, False, None | true, false, null |
undefined, NaN, Infinity | null |
| An ellipsis, or a key with no value | Dropped |
| A string or bracket the reply cut off | Closed |
| Text after the value | Dropped |
| A raw line break inside a string | Escaped |
| A repeated key in text that needed a repair | The later value, and a count |
A key named __proto__ is stored as a normal key.
What it leaves alone
Leading-zero numbers, hex numbers, and numbers with a leading plus are not repaired. The page shows an error near the spot it stopped.
Integers above 9007199254740991 stay numbers. JavaScript rounds those when it parses them, so the digits in the output can differ. The page says so. It does not turn the number into a string. Keep a large id as a string in the prompt if the digits matter.
Once the JSON parses, the JSON formatter is the place to pretty-print or minify a value that is already valid. The JSON to JSON Schema tool drafts a schema from a sample object. One JSON value per line belongs in the JSONL converter. The shape of JSON itself is in the introduction to JSON.
Try the JSON Repair
Repair invalid JSON from an LLM in your browser. Fixes code fences, trailing commas, single quotes, comments, and cut-off brackets, and lists each fix.
Related tools
- JSON Formatter & Validator: Validate, pretty-print, and minify JSON with clear error messages. Runs entirely in your browser.
- JSON to JSON Schema Generator: Paste sample JSON and get a JSON Schema with required keys and formats, plus a strict mode for OpenAI Structured Outputs.
- JSONL ↔ JSON Converter: Convert JSONL (JSON Lines, NDJSON) to a JSON array and back, with per-line validation, error positions, and key counts.
Want me to talk about your product? You can sponsor this site.