Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Utilities

JSON repair: fix broken JSON from LLMs

JSON repair turns almost-JSON into valid JSON. Paste broken JSON or a whole model reply and get parseable JSON back, with each fix shown: the 7 problems below include trailing commas, single quotes, unquoted keys, comments, Python literals, code fences and output cut off mid-object. Then format or minify it. It runs in your browser.

Try:

Repaired in your browser. Nothing you paste is sent anywhere.

Paste JSON to check and repair it.

Steps

How do I fix an invalid JSON error?

  1. Paste the broken JSON or the model’s whole reply into the box.
  2. Read the status: already valid, repaired, or the exact spot that needs a manual fix.
  3. Check any warnings, especially about cut-off output, and look over “What changed”.
  4. Choose a format, then copy or download the JSON.

Method

Why is my JSON not valid?

JSON has strict rules: double-quoted keys and strings, no trailing commas, no comments, and true, false and null in lower case. Language models break these rules often, because they learned from code and chat where the looser versions are normal. A single stray comma makes JSON.parse reject the whole response. To stop it happening at the source, read how to stop LLMs returning invalid JSON, or use the API’s structured outputs.

What do common JSON error messages mean?

The parser names the first character it couldn’t accept. These are the messages from Node.js 22 and Python 3.11:

  • Expected double-quoted property name (JavaScript) or Expecting property name enclosed in double quotes (Python): usually a trailing comma before }, a single-quoted key or an unquoted key.
  • Expected property name or '}' (JavaScript): a key in single quotes or without quotes straight after {.
  • Expected ',' or ']' after array element (JavaScript) or Expecting ',' delimiter (Python): a missing comma, or output that stops in the middle of a list.
  • Unexpected end of JSON input (JavaScript) or Expecting value (Python): empty text, or a reply cut off before the value was written.

How does the repair work?

First, if the JSON is wrapped in a Markdown code block or surrounded by sentences, the tool takes out just the JSON. Then the open-source jsonrepair library fixes the syntax: it adds missing quotes, turns single quotes into double quotes, removes trailing commas and comments, converts Python’s True, False and None, and closes brackets left open. The result is checked with your browser’s own JSON.parse before it’s shown.

Will it change my data without telling me?

Every edit appears in the “What changed” view, removed text struck through and added text highlighted. The tool also tells you when a repair needs a second look: when output was cut off and the end was closed for you, when several values were merged into an array, or when plain text was turned into a string because there was no JSON in it.

Repairing JSON in Python or JavaScript

For code that handles every response, use a library rather than this page: json_repair (the json-repair package on PyPI) in Python, or jsonrepair on npm, the library this tool runs, in JavaScript. A repair can make text parse, but it can’t restore data that was never written or decide what an ambiguous structure meant. If the repair fails here, the tool points to the line and column so you can fix that spot by hand. For fine-tuning files with one JSON object per line, validate a JSONL file instead.

Examples

What broken JSON can it repair?

ProblemModel outputRepaired
Trailing commas{"tags": ["a", "b",],}{"tags":["a","b"]}
Single quotes{'name': 'Ada'}{"name":"Ada"}
Unquoted keys{name: "Ada", age: 36}{"name":"Ada","age":36}
Comments{"retries": 3 // per request }{"retries":3}
Python values{'ok': True, 'error': None}{"ok":true,"error":null}
Code fence and chat textHere you go: ```json {"id": 7} ```{"id":7}
Cut-off output{"items": [{"id": 1}, {"id": 2{"items":[{"id":1},{"id":2}]}

FAQ

Frequently asked questions

What does invalid JSON mean?

The text breaks JSON’s grammar, defined in RFC 8259: for example a trailing comma, single quotes, an unquoted key, a comment, True instead of true, or a missing bracket. Any one of these makes JSON.parse in JavaScript or json.loads in Python reject the whole text, even if the rest is fine.

Is there a JSON repair library for Python or JavaScript?

Yes. In Python, pip install json-repair gives you the json_repair module; in JavaScript, jsonrepair on npm is the open-source library this page runs in your browser. Use a library in production code, and this page to inspect a single bad response and see exactly what was changed.

Why do language models produce invalid JSON?

A model writes JSON one token at a time, and much of what it learned from is JavaScript and Python, where single quotes, trailing commas, comments and unquoted keys are normal. Chat models also like to wrap JSON in a Markdown code block or add a sentence before it. And if the reply hits the max_tokens limit, it stops mid-value, leaving brackets unclosed.

How do I get valid JSON from the API in the first place?

Use the provider’s structured output or JSON mode where it exists: these constrain the model to valid JSON, and the strictest versions match a JSON Schema you supply. Set max_tokens high enough for the full answer, ask for JSON only with no explanation, and still validate the result in your code, because a stopped or filtered response can be incomplete.

What can’t it repair?

It fixes syntax, not meaning. If output was cut off, the missing data is gone: the tool closes the open brackets so the rest parses, and tells you it did. If brackets are badly mismatched in the middle, or a value is ambiguous, it may stop and show you the line and column to fix by hand.

Is my data sent anywhere?

No. The repair runs in your browser with the open-source jsonrepair library, so pasted data, including any personal or confidential fields, stays on your device.

Does it handle JSON Lines (NDJSON)?

Yes: several JSON values one after another are combined into a single array, and the tool tells you it did that so you can decide whether that’s what you want.

What’s the difference between validating and repairing JSON?

Validating only tells you whether text is valid JSON and where the first error is; the “Why it was invalid” tab shows exactly that, from your browser’s own JSON.parse. Repairing changes the text so it becomes valid. The “What changed” tab shows every edit, so nothing is changed without you seeing it.