JSON Repair
Turn broken, fenced or truncated JSON — especially LLM output — into valid RFC 8259 JSON.
Broken JSON
Repaired JSON
The single largest source of malformed JSON today is a language model. You ask for JSON, you get Sure! Here is the JSON you requested: followed by a markdown code fence, single-quoted keys, a trailing comma, a stray NaN where a number should be, and — if the response hit its token limit — no closing brace at all. Standard formatters simply refuse the whole thing, which is technically correct and completely useless when you have a hundred of these to process.
This tool is built for exactly that input. It strips the fence and any leading prose first, then runs the jsonrepair library over what is left to fix the structural damage, then parses the result with native JSON.parse so you know the output is genuinely valid rather than merely improved. It tells you what it changed, so you can decide whether a repair was a fix or a guess.
How to use the json repair
- 1
Paste the broken output verbatim
Do not clean it up first. Leave the
```jsonfence, the "Here is the JSON:" preamble and the trailing apology in place — removing that wrapper is the first thing the tool does. - 2
Repair
The result appears in the output pane along with a list of notes describing each stage that fired: fence removed, prose discarded, syntax repaired. If nothing needed fixing, it says so rather than pretending to have done work.
- 3
Check the repair, then take the output
Read the notes and skim the result before you trust it, especially if the input was truncated. Choose your indentation, then copy or download clean JSON.
What it fixes
The repair runs in two stages. First, the wrapper is removed:
- A surrounding markdown code fence —
```json,```json5,```jsoncor a bare```— is stripped. - Leading prose before the first
{or[is discarded, which handles the conversational preamble that models add even when told not to.
Then the jsonrepair library repairs the JSON itself:
- Trailing commas before
}or]are removed. - Single-quoted strings and keys are converted to double quotes, along with smart quotes pasted in from elsewhere.
- Unquoted keys —
{name: "x"}— get their quotes. - Comments, both
//and/* */, are stripped. - Non-JSON literals are converted: Python-style
TrueandFalsebecometrueandfalse,Nonebecomesnull, andNaNandInfinity— which have no JSON representation at all — are preserved as the quoted strings"NaN"and"Infinity"rather than being silently dropped. - Truncated documents are closed: unterminated strings, objects and arrays get the delimiters they are missing.
- Missing commas between adjacent objects or array items are inserted, and concatenated JSON fragments are joined into an array.
Repairing truncated LLM output
Truncation is the failure mode worth thinking hardest about. When a model runs out of output tokens mid-object, the repair closes every open string, object and array so the document parses — but it cannot invent the values that were never generated. You get syntactically valid JSON that is semantically incomplete: the last record may be missing half its fields.
That is usually the right trade. A parseable document with a short final record can be inspected, logged and partially salvaged; an unparseable one cannot. But treat a truncation repair as a signal that your generation ran out of room, not as a fix. The durable answers are raising the output token limit, requesting fewer records per call, or using structured-output or function-calling modes where the provider constrains generation to a schema and cannot emit a fence or a preamble in the first place.
The same applies to a model that stopped mid-sentence inside a string value. The string will be closed at the truncation point, and the text simply ends there.
When repair is the wrong tool
Repair is a rescue operation, not a workflow. If the same source produces broken JSON every time, fix the source: it is cheaper and it does not silently reshape your data.
There is also a class of input that no repairer should touch. If the fault is a genuine ambiguity — a value that could plausibly be a string or a number, a structure where a missing comma could be inserted in two different places — any automatic fix is a guess, and a guess that produces valid JSON is more dangerous than a loud failure, because nothing downstream will question it. Read the notes, and eyeball the output against the input when the document matters.
Input that is too badly damaged to yield valid JSON at all is rejected with an explicit message rather than returned half-repaired.
JSON Repair FAQ
Will the repaired JSON always be valid?
Yes, or you get an error. The output is parsed with native JSON.parse after repair, so anything returned is RFC 8259 valid. If the input cannot be repaired into something that parses, the tool says the input is too damaged rather than handing you a plausible-looking string.
Can it fix JSON that was cut off mid-way?
It can make it parse — unterminated strings, objects and arrays are closed. It cannot recover data that was never present, so the tail of the document will be incomplete. Check the last record before relying on it.
Does it handle JSON5, JSONC and Python dict output?
In practice, most of it. Comments, trailing commas, unquoted keys and single quotes are all repaired, which covers JSON5 and JSONC. Python-style True, False and None are converted too. Python dicts containing tuples or datetime(...) calls are not JSON in any sense and will fail.
Will it change my data?
It changes syntax, not values — except where a value had no legal JSON form to begin with. NaN and Infinity come back as the strings "NaN" and "Infinity", so a numeric field can change type; that is visible in the output rather than hidden. Duplicate keys collapse to the last one when the repaired text is parsed. Everything else survives unchanged.
Is the broken JSON I paste sent anywhere?
No. Fence stripping, repair and parsing all run in a Web Worker in your browser. LLM output frequently contains customer data that was passed into the prompt, which is precisely why this tool has no server component.
