How to Repair Malformed & Truncated LLM JSON Outputs Locally
Automatically strip markdown code fences, fix single quotes, trailing commas, Python True/False literals, and truncated brackets from LLM JSON responses.
Even capable LLMs occasionally wrap JSON in ```json markdown fences, emit JavaScript comments, use Python True/None literals, leave trailing commas, or hit max-token truncation before closing brackets. Repair LLM JSON recovers valid RFC 8259 JSON in milliseconds inside browser RAM.
The 5 most common ways LLM JSON breaks in production
Standard JSON.parse() fails immediately on minor syntax deviations. Across production AI logs, five failure patterns account for nearly every parse exception:
- Markdown wrappers: leading
Here is the JSON:prose and```json...```fences - Unquoted keys or single-quoted strings (
'status': 'ok') - Inline
//or/* */comments added by reasoning models - Trailing commas after the final array item or object property
- Max-token cutoff leaving unclosed
[or{brackets at EOF
Recovery + AJV schema verification pipeline
After repairing syntax with jsonrepair AST recovery, immediately verify structural compliance by handing the repaired JSON straight into JSON Schema Validator or converting it to TOON/Zod via DataTo's local IndexedDB pipeline.
```json
{
// Cut off mid-stream
'status': 'ok',
'items': [{id: 1, active: True,}
```Tools used in this guide
LLM & TOON →Fix broken AI JSON outputs: strip markdown code fences, repair single quotes, comments, trailing commas, and unclosed brackets.
JSON FormatterPretty-print, minify, sort keys, auto-repair trailing commas, and browse collapsible JSON trees in browser RAM.
JSON Schema ValidatorValidate payloads against Draft 2020-12 or Draft-07 schemas locally with AJV and inspect exact JSON Pointer error paths.
JSON to OpenAI FunctionConvert sample JSON into strict OpenAI response_format (strict: true, additionalProperties: false) or Anthropic Tool Use schemas.
Related formats
Related Comparisons
Frequently asked questions
Does Repair LLM JSON work on truncated streaming responses?
Yes. Missing closing quotes, arrays, and braces at the end of a cut-off response are automatically balanced into parseable JSON.
Will it alter valid strings inside JSON values?
When fences are stripped first and valid delimiters remain intact, jsonrepair modifies only invalid structural tokens.
How can I prevent LLMs from producing broken JSON in the first place?
Use strict Structured Outputs schemas generated with DataTo JSON to OpenAI Function so constrained decoding guarantees valid syntax.
Authoritative References
More engineering guides
Turn internal OpenAPI 3.x YAML/JSON specs into Model Context Protocol (MCP) TypeScript tools and Zod v4 input schemas locally without uploading internal APIs.
Structural JSON Diff & Primary-Key CSV Diff for Data ValidationCompare API payloads and database table exports cleanly using key-order-normalized structural JSON diff and primary-key-aligned CSV diff locally.
JSON Schema Draft 2020-12 to Zod v4, Pydantic v2 & TypeScriptCompile Draft 2020-12 and Draft-07 JSON Schemas with $defs, $ref, enums, and bounds into Zod v4, Pydantic v2, and TypeScript interfaces locally via AJV & IR.
JSON Schema vs. Zod v4 vs. Pydantic v2 for LLM Structured OutputsHow OpenAI Strict Mode, Anthropic Tool Use, Zod v4, and Pydantic v2 map through a Unified Type IR—and how to generate all three locally.