Repair JSON Generated by ChatGPT, Claude & Other LLMs

Paste the whole reply from ChatGPT, Claude, Gemini, Llama or any other model, including the ```json fence and the “Here is your JSON” chatter. You get valid JSON back and a list of every fix that was made. Truncated output, Python-style dicts and stringified JSON are handled too. Nothing is uploaded.

Repaired JSON

        
Validate against a JSON Schema (optional)

Paste the schema you asked the model to follow. A bare schema works, and so does an OpenAI response_format / json_schema wrapper, an Anthropic tool with input_schema, or a Gemini responseSchema. Checked keywords: type, required, properties, additionalProperties, items, enum, const, minLength/maxLength, minimum/maximum, pattern, minItems/maxItems, uniqueItems, anyOf/oneOf/allOf and local $ref.

How to fix JSON from an LLM

  1. Paste the reply as it is. You don't need to cut out the JSON first. Code fences (```json, ```, ~~~), the sentence before the JSON and the “Let me know if you need anything else” after it are all removed.
  2. Read the fix list. Every change is listed with a count and, where possible, the line and column of the first occurrence in the extracted JSON. You can see exactly what was changed, for example a closed string where the reply was cut off.
  3. Pick a value if there are several. If the reply contains more than one code block or JSON object (an “input” and an “output” example, say), they are wrapped in an array by default. Use the picker to keep just one.
  4. Optionally validate. Open Validate against a JSON Schema and paste the schema you gave the model. Each violation is listed with its JSON path, such as $.items[2].price.

Why LLMs break JSON

A language model writes JSON one token at a time, predicting what usually comes next. Unless the API constrains decoding to a grammar or schema, nothing checks that the brackets balance or the quotes match. The same few failures come up again and again:

LLM JSON failures and what this tool does

What you seeExampleFix applied
Markdown code fence```json { ... } ```Fence removed. Several blocks: pick one or keep all as an array
Prose around the JSONHere is the JSON: {...} Hope this helps!The first balanced {...} or [...] is extracted, ignoring brackets inside strings
Cut off by the token limit[{"id": 1}, {"id": 2, "name": "ChOpen string closed, half-written tru completed, dangling key set to null, brackets closed
Stringified JSON"{\"a\": 1}"Unwrapped, including double-escaped \\\"
Python dict{'ok': True, 'v': None, 'p': (1, 2)}Double quotes, true/null, tuples to arrays
Trailing or missing commas{"a": 1 "b": 2,}Comma inserted, trailing comma removed
Unquoted keys, comments{name: "x" // label}Keys quoted, comments removed
Smart quotes{“name”: “José”}Straight quotes
NaN, undefined, Infinity{"score": NaN}Replaced with null
Skipped content[1, 2, ...]... removed (or null after a key)
Unescaped inner quotes{"q": "He said "hi" to me"}Inner quotes escaped as \"
Several objects{"a":1} {"a":2}Wrapped in an array, or keep one

Large integers such as 1453729827150749696 are kept exactly as written. JSON.parse would round them, which silently corrupts IDs.

Worked example: a truncated reply

Here are the extracted records:

```json
[
  {"id": 1, "title": "Intro to Rust", "authors": ["Ana", "Ben"]},
  {"id": 2, "title": "Async in depth", "authors": ["Chen", "Dev

becomes valid JSON, with four fixes listed (fence stripped, prose removed, a cut-off string closed, three brackets closed):

[
  { "id": 1, "title": "Intro to Rust", "authors": ["Ana", "Ben"] },
  { "id": 2, "title": "Async in depth", "authors": ["Chen", "Dev"] }
]

Repair cannot recover what the model never wrote. "Dev" is probably an incomplete name, and any records after it are missing. Use the repaired output to salvage a partial result or to debug. For production, fix the cause (see below) and re-request.

How to stop LLMs returning broken JSON

Repair is a safety net. Each major API has a feature that makes valid, schema-shaped JSON much more likely. Names and parameters change between versions, so check the provider's current documentation.

OpenAI (ChatGPT models)

Structured Outputs constrains generation to a JSON Schema you supply. In Chat Completions you pass it as response_format with type json_schema and strict: true. Strict mode expects every property to be listed in required and additionalProperties: false. The older JSON mode ({"type": "json_object"}) only guarantees syntactically valid JSON, not your shape.

response_format={
    "type": "json_schema",
    "json_schema": {"name": "invoice", "strict": True, "schema": INVOICE_SCHEMA},
}

Anthropic (Claude)

The dependable route is tool use. Define a tool whose input_schema is your JSON Schema, force it with tool_choice, and read the tool call's input, which the API returns already parsed. Newer Claude models also offer a structured-outputs option that constrains the response to a schema; check which models support it. With plain prompting, describe the exact shape, ask for JSON only with no commentary, and, where the model supports prefilling, start the assistant turn with { so the reply begins inside the object.

tools=[{"name": "record_invoice", "description": "Save the extracted invoice",
        "input_schema": INVOICE_SCHEMA}],
tool_choice={"type": "tool", "name": "record_invoice"}

Google Gemini

Set the response MIME type to application/json in the generation config and supply a responseSchema. The schema format is a subset of OpenAPI, so types are often written in upper case ("STRING", "OBJECT"). The validator above accepts both cases.

Checks that apply to every model

Repair LLM JSON in code

Python: json-repair

# pip install json-repair
import re, json_repair

def parse_llm_json(reply: str):
    m = re.search(r"```(?:json)?\s*(.*?)```", reply, re.S)   # prefer a fenced block
    text = m.group(1) if m else reply
    start = min([i for i in (text.find("{"), text.find("[")) if i >= 0], default=0)
    return json_repair.loads(text[start:])   # repairs quotes, commas, truncation

data = parse_llm_json(response_text)

json_repair.repair_json(text) returns the fixed JSON as a string instead of a Python object.

JavaScript / TypeScript: jsonrepair

// npm install jsonrepair
import { jsonrepair } from "jsonrepair";

function parseLlmJson(reply) {
  const fenced = reply.match(/```(?:json)?\s*([\s\S]*?)```/);
  let text = fenced ? fenced[1] : reply;
  const start = text.search(/[\[{]/);
  if (start > 0) text = text.slice(start);
  return JSON.parse(jsonrepair(text));     // throws if it cannot be repaired
}

Both libraries fix syntax. Neither knows your schema, so validate the parsed object with a schema validator (for example jsonschema in Python, Ajv in JavaScript) or Pydantic/Zod before trusting it.

Frequently asked questions

Why does ChatGPT return invalid JSON?

A language model writes JSON one token at a time and nothing forces the result to be valid unless you turn on a structured-output feature. The usual problems are a markdown code fence around the JSON, a sentence of explanation before or after it, output cut off at the token limit, Python habits such as single quotes and True or None, trailing commas, comments, and ... placeholders where the model skipped content.

How do I fix JSON that was cut off mid-response?

Paste it here. The tool closes the open string, completes a half-written true, false or null, fills a dangling key with null and adds the missing closing brackets, so you get valid JSON with everything up to the cut. The last item may be incomplete, so check it. To avoid truncation, raise the maximum output tokens, ask for fewer items per call, and check the finish reason the API returns.

Why is my JSON wrapped in quotes and full of backslashes?

The JSON was serialised twice: something called json.dumps or JSON.stringify on a value that was already a JSON string, so every quote became \". This is common with tool-call arguments, logs and queues. The tool detects one or more layers of escaping and unwraps them. In your own code, parse the string once more instead of stringifying it again.

What is the difference between JSON mode and structured outputs?

JSON mode only promises syntactically valid JSON; the keys and types can still be anything. Structured outputs constrain generation to a JSON Schema you supply, so required fields, types and enums are respected. Even with either one, a response can stop early at the token limit, so check the finish reason and validate before use.

How do I get Claude to return only JSON?

The most reliable way is tool use: define a tool whose input_schema is your JSON Schema and force it with tool_choice, then read the tool input, which arrives as a parsed object. Newer Claude models also offer a structured-outputs option; check the current API docs for which models support it. In plain prompting, show the exact shape, ask for JSON only with no prose, and, where the model supports it, prefill the assistant turn with an opening brace.

Can I repair LLM JSON in Python or JavaScript code?

Yes. In Python, pip install json-repair and call json_repair.loads(text) or repair_json(text). In JavaScript, npm install jsonrepair and call jsonrepair(text) before JSON.parse. Both fix syntax; you may still want to cut the JSON out of surrounding prose first, as shown in the code examples on this page.

Is my data uploaded anywhere?

No. Extraction, repair, formatting and schema validation all run in JavaScript in your browser. Nothing you paste is sent to a server or to any AI model, which matters when the reply contains customer data or API keys.