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.

How to use this resource

  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

FAQ

Why won't the JSON from my AI model parse even though the response looks complete?

Broken model JSON usually fails on one deterministic culprit: a trailing comma before } or ], single-quoted keys, or an unclosed bracket. The validator runs a real JSON.parse, and when it throws, the cause-sniffer names which of those three broke it — so an opaque 'Unexpected token' error becomes one specific, actionable syntax fix.

Will the repair prompt fix my broken JSON or make me regenerate the whole thing?

It repairs in place — it never regenerates, so your extracted values aren't re-rolled. Paste the model's raw unparseable response, and the validator hands you a REPAIR TASK / PROBLEMS FOUND / OUTPUT RULES prompt telling the model to 'keep everything that was already correct unchanged; fix only the problems listed.' You run that in ChatGPT, Claude, or Gemini.

Does NewPrompt fix the invalid JSON for me automatically, or just tell me what's wrong?

It detects the cause; the model does the repair. In the browser, a genuine JSON.parse runs and names the culprit deterministically — trailing comma, single quotes, or unclosed bracket, weighted by a 50-point parse penalty. NewPrompt never silently rewrites the JSON; the actual fix happens when you paste the repair prompt into the assistant you choose. To prevent invalid JSON up front, use the JSON Output Prompt Builder.

More resources from AI Output Validator

Resources that pair well

Related tools

Workflows that use this resource

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