Force JSON Output from AI
Stop getting 'Sure, here is the JSON…' — the output-contract pattern that forces models to return only parseable JSON: schema, example, and a strict rule block.
```json fences are the most common reason JSON.parse fails on model output. The rule set that prevents them — and why fences happen in the first place.
Models wrap JSON in markdown fences because their training rewards readable chat answers — a fenced code block IS the polite way to show JSON to a human. Your parser disagrees. The fix is naming the failure mode explicitly: a generic 'return JSON' keeps the fences; 'do not wrap the output in markdown' and 'do not use code fences' as separate rules removes them. This resource loads a webhook-bound contact-form contract in Strict mode, where the anti-fence rules ride alongside the schema — the configuration for output that goes straight into JSON.parse.
Read the two anti-fence rules
'Do not wrap the output in markdown' and 'do not use code fences' are separate rules on purpose — models treat them as different behaviors.
Note the example is unfenced
The example response shows bare JSON. If your example is fenced, the model fences — examples outvote rules.
Adopt the contract
Swap in your fields and keep Strict mode for anything that pipes into a parser.
Models treat markdown wrapping and code fences as different behaviors, so the OUTPUT RULES name each failure mode explicitly instead of bundling them. A single 'return JSON only' leaves the ```json fence intact because fencing feels like polite formatting, not a violation. Splitting them into two prohibitions removes the fence and the surrounding markdown separately.
The builder outputs the prompt text; you run it in your own assistant, and the OUTPUT RULES line 'Output only valid JSON — nothing else' is what aims the response at JSON.parse directly. NewPrompt does not run the model or strip fences, so you still parse and review the contact-form object against its schema (full_name, email, subject, message, urgency) before trusting it.
Check that your EXAMPLE OF A VALID RESPONSE is bare JSON, not fenced. Examples outvote rules, so a fenced example teaches the model to fence even with 'Do not use code fences' present. The template deliberately shows the full_name/email/subject example unfenced; if you swapped in your own fields inside a ```json block, the fence comes back.
Stop getting 'Sure, here is the JSON…' — the output-contract pattern that forces models to return only parseable JSON: schema, example, and a strict rule block.
Native JSON modes guarantee syntax, not your schema. The prompt contract that covers field names, types, and null discipline — whether or not the API has JSON mode.
When the destination is a spreadsheet, CSV is the contract: one header row, one data row, quoting rules, and an honest answer about nested data.
Free text in, named fields out. The extraction prompt pattern that turns any unstructured text into consistent, parseable records.
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.
'Make it good', 'be detailed', 'keep it interesting' — vague prompts get vague output. The fix is mechanical: replace every fuzzy word with a checkable instruction.
Build prompts that return structured data — JSON first, with YAML, XML, and CSV modes — parseable every time.
AI models return broken JSON more often than you'd expect. Here's how to structure a prompt so the output parses cleanly, plus what to check before you trust it.