Engineering JSON Output Validation

Fix Invalid JSON from AI

The JSON won't parse and you can't see why. Deterministic cause-sniffing — trailing commas, single quotes, unclosed brackets — and the repair prompt that fixes it.

Overview

Broken JSON from a model fails silently in the worst way: the response looks complete, and the parser just says no. This setup loads the classic case — a contract extraction with a trailing comma before the closing brace — and shows the validator's parse-failure path: JSON.parse fails, the cause-sniffer identifies the likely culprit deterministically (trailing comma, single-quoted keys, or an unclosed bracket), and the repair prompt tells the model exactly what to fix while keeping every extracted value unchanged.

Workflow

  1. Paste the unparseable response

    The validator attempts a real parse, then sniffs the cause when it fails.

  2. Check the likely cause

    Trailing comma, single quotes, unclosed bracket — named deterministically, not guessed.

  3. Repair, don't regenerate

    The repair prompt keeps every correct value and fixes only the syntax — regeneration would re-roll the data too.

Why This Works

  • Cause-sniffing turns an opaque parse error into an actionable fix
  • Repairing preserves extracted data that regeneration would scramble
  • A 50-point parse penalty makes broken syntax unmissable in the score

Best for

  • Developers staring at "Unexpected token" errors
  • Pipelines that store raw model output for later parsing
  • Anyone whose extraction came back 99% right and 100% unparseable

Not for

  • Preventing invalid JSON up front — that's the JSON Output Prompt Builder's strictness rules
  • Repairing the JSON by hand — the repair prompt makes the model do it

Use cases

  • Diagnosing why a model's JSON fails JSON.parse
  • Identifying trailing commas and quote problems instantly
  • Repairing the response without losing the extracted values

Tip: Save time by exploring related resources and tools that integrate with this workflow.

Explore all resources