Repair LLM JSON Output: Handling Almost-JSON from AI
LLMs frequently return JSON wrapped in ```json fences, with single quotes, Python literals, or unquoted keys. Repair the syntax, validate strictly, then derive TypeScript types from the cleaned output.
When you land here
An LLM returned JSON that JSON.parse rejects. The most common offenders are markdown code fences wrapping the document, single quotes, unquoted keys, Python literals like True/None, and stringified objects. Repair the syntax, then validate strictly before you hand the result to your app.
The repair workflow for LLM output
Run repair first — it tolerates fences, single quotes, unquoted keys, Python literals, and trailing commas. Validate second to confirm the cleaned text round-trips. Use the cleaned tree to derive types or to feed your app.
Once it parses, derive types
After the LLM's output validates, derive TypeScript interfaces or a JSON Schema so the next call has a contract to validate against. That's the difference between repairing once and repairing forever.
What's coming in standards
Two upcoming JavaScript proposals matter for LLM JSON workflows: JSON.parse source-access (read errors with the original text alongside the value) and JSON.parseImmutable. They reduce the amount of bespoke repair code that lives in every app.
Recommended path
Paste the LLM output, clean it, type it.
- 1. Tool: / — paste the AI output, click Repair & Format.
- 2. Guide: /guides/repair-llm-json-output — handles fences, quotes, Python literals.
- 3. Blog: /blog/repair-broken-json-in-javascript — the same workflow in code.
- 4. Reference: /news/json-parse-source-access-baseline-2025 — what standard support is arriving.
JSON repair guides
Topic hubs
Specific guides
- How to Decode Base64 Strings (and JWT Payloads)
- URL Encoding: Percent-Encode Query Parameters and Paths
- Convert YAML to JSON (and Avoid Indentation Errors)
- Convert JSON to CSV: Flatten an Array of Objects
- Convert JSON to XML: Root Elements, Attributes, and Arrays
- Escape JSON as a String Literal (and Decode Double-Encoded JSON)
- Fix Trailing Comma in JSON
- Fix Single Quotes in JSON
- Fix Unquoted Keys in JSON
- Repair LLM JSON Output
- Fix JSON Parse Error: Expected Property Name
- JSON vs JS Object Literal: The Key Differences
- Validate JSON Before API Requests
- JSON Formatter vs JSON Repair
- Fix JSON Unexpected Token Errors
- JSON to JavaScript Object Converter