DevKits

JSON Repair — Fix Broken JSON, LLM Output & Trailing Commas

Automatically fix invalid JSON — trailing commas, single quotes, unquoted keys, comments, markdown code fences, and Python-style True/False/None. Purpose-built for LLM output. 100% local.

Last updated:

228 chars · 18 lines
No output yet.

Could not repair: Expected property name or '}' in JSON at position 7 (line 3 column 3)

Line 3, column 3

Partial fixes applied before failure:

  • Trimmed leading/trailing non-JSON text.
  • Stripped // line comments.
  • Converted single-quoted strings to double-quoted.
  • Quoted bare (unquoted) object keys.
  • Replaced special literal True with true.
  • Removed trailing commas.
  • Replaced special literal None with null.
  • Replaced special literal NaN with null.

Paste broken or almost-JSON text — including LLM output with markdown fences, single quotes, trailing commas, unquoted keys, comments, or Python-style True/False/None — and get back strict, RFC 8259 compliant JSON. Every fix is listed so you can audit before shipping.

What is JSON Repair?

A JSON repair tool takes text that is 'almost JSON' and rewrites it into strictly valid JSON. It became a critical piece of the LLM stack because language models routinely emit JSON wrapped in markdown fences, sprinkled with trailing commas, or seasoned with Python-style literals like True and None. Standard JSON.parse rejects all of those. Rather than manually strip fences and re-quote keys every time, this tool applies a well-defined chain of transformations — strip fences, normalize smart quotes, extract the first balanced object/array, then lex-scan to remove comments, quote bare keys, drop trailing commas, and coerce special literals — and re-serializes the result through JSON.stringify so the output is guaranteed to be spec-compliant.

How to repair broken JSON online

  1. 1Paste the broken JSON — LLM output, JSON5, JSONC, or a hand-edited config — into the left editor.
  2. 2The right pane instantly shows the repaired JSON, formatted with 2-space indent.
  3. 3Review the list of applied fixes in the status card. Every transformation is disclosed by name (trailing commas removed, single quotes converted, etc.).
  4. 4If the input can't be salvaged, the tool shows a parser error with line and column pointing at the first unrecoverable syntax problem, plus the partial fixes it did manage to apply.
  5. 5Copy the repaired output or switch to Tree view for a structural preview before shipping.

Use Cases

LLM structured output cleanup

GPT-4, Claude, and Gemini all sometimes emit JSON wrapped in ```json fences with trailing commas or single quotes. Feed the raw completion into this tool before JSON.parse.

Migrate JSON5 / JSONC to strict JSON

Config files that started as JSON5 (comments, unquoted keys) need to be strict JSON for some parsers. This tool strips extensions in one pass.

Fix hand-edited JSON

Trailing commas and dropped double quotes are the most common typos when hand-editing JSON. Paste the file to see and confirm each fix.

Salvage log lines and error dumps

Applications sometimes log JSON with surrounding text ('Received response: { ... }'). The tool extracts the first balanced JSON block and repairs it.

Code Examples

Input (LLM output)

```json
{
  // The user's profile
  name: 'Alice',
  admin: True,
  tags: ['dev', 'design',],
  score: NaN,
}
```

Repaired output

{
  "name": "Alice",
  "admin": true,
  "tags": [
    "dev",
    "design"
  ],
  "score": null
}

Use in a Node.js pipeline

import { repairJson } from "./repair.js";

const raw = await callLLM({ prompt });
const { ok, output, patches } = repairJson(raw);
if (!ok) throw new Error("LLM output unrecoverable");
const payload = JSON.parse(output);
console.log(`repaired with ${patches.length} fixes`);

Key Concepts

RFC 8259
The current JSON specification. Requires double-quoted strings, no trailing commas, no comments, and no NaN/Infinity. Everything the repair tool touches is a deviation from this spec.
JSON5
A superset of JSON that allows single quotes, unquoted keys, comments, trailing commas, and NaN/Infinity. The repair tool essentially converts JSON5 to strict JSON, losing only the special float values (coerced to null).
JSONC
'JSON with Comments' — a strict-JSON superset used by VS Code (tsconfig.json, .vscode/settings.json) that permits // and /* */ comments. Stripping comments is one of the tool's core steps.
Structured output
Modern LLM APIs expose 'JSON mode' or 'response_format' options that constrain output to valid JSON. When those aren't available or return malformed data, this tool acts as a fallback layer before parsing.

Tips & Best Practices

  • Never trust LLM output blindly — after repair, still validate against a JSON Schema for the fields you actually care about.
  • If you control the prompt, ask the model to output raw JSON without markdown fences. Repair is a safety net, not a workaround for weak prompting.
  • The repair tool coerces NaN and Infinity to null. If those values are semantically important, encode them as strings ("NaN") in your schema instead.
  • Bare-key detection only fires when the identifier is immediately followed by a colon in an object context. Standalone identifiers like `true` / `false` / `null` are preserved as JSON literals.
  • For very large LLM outputs, batch through the tool in server-side JavaScript rather than the browser to avoid main-thread jank.

Frequently Asked Questions

What kinds of broken JSON can it fix?

Common issues from LLM output and hand-written config: markdown code fences (```json ... ```), single-quoted strings, unquoted object keys, trailing commas, // and /* */ comments, smart / curly quotes, Python-style True/False/None, and JavaScript-only literals like NaN, Infinity, and undefined (rewritten to null). It also trims stray text before or after the JSON block.

Is the repair lossy?

Structural fixes (removing commas, adding quotes) are lossless. Value coercion is spec-driven: True → true, None → null, NaN / Infinity → null. Every fix is listed in the report so you can review before shipping.

Why is this useful for LLM output?

Language models regularly emit JSON wrapped in markdown fences, with trailing commas, single quotes, or comments — none of which parse with JSON.parse. This tool handles those cases in one pass so you can pipe raw model output into strict JSON consumers.

Does the repaired output pass strict JSON.parse?

Yes. The tool re-serializes with JSON.stringify after parsing, so the output is guaranteed to be RFC 8259 compliant. If it can't produce valid JSON, the error and the partial repairs are shown instead.

Is my JSON uploaded anywhere?

No. All parsing and repair runs entirely in your browser as client-side JavaScript. Nothing is uploaded, logged, or stored.

Related Tools