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.