How to Prompt AI for Structured JSON Output
Why does AI keep wrapping your JSON in markdown fences? These ai json output prompts show you how to get clean, parseable data every time, with schema tricks that stop pipelines from breaking.
Extract the following information from this product review and return it as JSON only.
Review: "This blender is incredible for the price. Motor is powerful, but the lid seal leaks a little after six months of daily use. Still, I'd buy it again — 4 out of 5 stars."
Return ONLY valid JSON matching this exact schema, with no markdown code fences, no explanatory text before or after, and no additional commentary:
{
"sentiment": "positive | negative | mixed",
"rating": number,
"pros": ["array of strings"],
"cons": ["array of strings"],
"would_repurchase": boolean
}Why does AI keep wrapping your JSON in markdown fences even after you asked it not to? If you've ever built an automation that pipes AI output straight into a script, you already know this problem: you ask for clean data, you get clean data wrapped in three backticks and the word "json," and your parser throws an error at 2am when nobody's watching the pipeline.
Getting reliable ai json output prompts isn't about asking nicely. It's about understanding exactly what the model is trained to do by default — helpful, human-readable formatting — and explicitly overriding that default with a structure it can't misinterpret.
What is Structured JSON Output?
Structured JSON output means the model returns data in a strict, parseable format — key-value pairs, arrays, nested objects — instead of prose. This matters any time AI output needs to feed into another system: a database, a spreadsheet import, a web app, or an automation tool like Zapier or Make. A human doesn't need JSON. A script does.
The core challenge is that language models are trained heavily on conversational, explanatory text. Left to their own devices, they'll often add a friendly sentence before the JSON ("Sure! Here's the structured data you requested:") or wrap it in code fences for readability. Both of these break automated parsing, and both are entirely preventable with the right prompt.
Why It Matters
A broken JSON pipeline doesn't fail loudly and helpfully — it fails silently or with a cryptic parser error, and figuring out that the actual bug is "the model added a sentence before the brace" can eat 20-30 minutes of debugging time for something that takes 10 seconds to fix at the prompt level.
⚡ Pro tip: if you're building anything that runs unattended — a scheduled script, a webhook handler, a batch job — treat "does this reliably return parseable JSON with zero exceptions" as a hard requirement before you ship it, not a nice-to-have you'll fix later.
Getting Clean, Parseable JSON
Here's a prompt structure that reliably produces clean JSON across Claude, ChatGPT, and Gemini:
Extract the following information from this product review and return it as JSON only.
Review: "This blender is incredible for the price. Motor is powerful, but the lid seal leaks a little after six months of daily use. Still, I'd buy it again — 4 out of 5 stars."
Return ONLY valid JSON matching this exact schema, with no markdown code fences, no explanatory text before or after, and no additional commentary:
{
"sentiment": "positive | negative | mixed",
"rating": number,
"pros": ["array of strings"],
"cons": ["array of strings"],
"would_repurchase": boolean
}What this does: the phrase "no markdown code fences, no explanatory text before or after" directly counters the model's default conversational formatting habit, and providing the exact schema — with types specified — removes ambiguity about field names, capitalization, and data types that would otherwise vary between runs.
⚡ Pro tip: always specify the data type for each field (string, number, boolean, array) directly in the schema example. Without this, you'll get inconsistent output — sometimes a rating comes back as
"4"4Handling Edge Cases and Nested Data
Real-world data is messier than a single clean review. Here's a prompt for nested structures, which trips up a lot of people the first time they try it:
Extract structured data from this customer support ticket and return it as JSON only, no markdown fences, no extra text.
Ticket: "Hi, I ordered two items (order #48291) — the blue jacket arrived fine but the running shoes were the wrong size (ordered 10, got 8). I need a replacement or refund for the shoes, and I'd like this resolved by Friday."
Schema:
{
"order_id": "string",
"items": [
{"item": "string", "issue": "string or null", "resolution_requested": "string or null"}
],
"urgency": "low | medium | high",
"deadline_mentioned": "string or null"
}What this does: giving an explicit "string or null" option for optional fields prevents the model from either omitting the field entirely or inventing a placeholder value when data genuinely isn't present — both of which break a strict parser expecting a consistent shape.
⚠️ Common mistake: not specifying what happens when a field has no data. If your schema doesn't say "string or null" explicitly, some models will just leave the field out of the response entirely, which crashes any code that assumes every field always exists.
Real-world scenario — e-commerce operations manager: an operations manager at a mid-size online retailer built an automation that classifies incoming support tickets using AI-generated JSON, feeding priority and category directly into their ticketing system. Their first version crashed roughly 1 in 20 times because the schema didn't specify null handling for optional fields. Adding explicit "string or null" typing to every optional field dropped that failure rate to effectively zero over the following month.
Validating and Repairing Output
Even a well-designed prompt occasionally produces slightly malformed JSON — a trailing comma, a missing quote. Build a repair step into your automation rather than assuming every response will parse cleanly on the first try.
The following text should be valid JSON but failed to parse. Fix any syntax errors and return only the corrected, valid JSON — no explanation, no markdown fences.
[paste malformed JSON here]What this does: it uses the model's strength at pattern recognition to catch the kind of small syntax slip a strict parser can't recover from on its own, without requiring you to write custom repair logic in your own code.
⚡ Pro tip: in production systems, wrap your JSON parsing in a try/except (or try/catch) block, and if it fails on the first attempt, automatically send the malformed output back through a repair prompt before giving up. This two-step approach catches the vast majority of formatting slips without any manual intervention.
Real-world scenario — data analyst at a logistics company: a data analyst automating shipment status extraction from carrier emails found that about 3% of responses had a stray trailing comma the strict JSON parser rejected. Rather than rewriting the whole prompt, adding a one-line repair-prompt fallback in the pipeline resolved essentially all of the failures without any change to the original extraction prompt.
Common Mistakes
Beyond the null-handling issue above, a few other patterns cause recurring problems.
Asking for JSON "if possible" or "when appropriate" instead of as a hard requirement. Soft language gets soft compliance — the model treats it as a suggestion, not a rule, and will sometimes revert to prose, especially for edge cases.
Forgetting to specify enum values as literal options. If you want a "status" field limited to specific values, spell them out —
"status": "open | in_progress | resolved""status": "string"Real-world scenario — product manager at a SaaS company: a PM building an AI-powered feedback categorization tool got wildly inconsistent category labels — "Bug," "bug," "Bug Report" — until the prompt explicitly listed the exact allowed enum values. And, actually, this one fix alone resolved about 80% of the downstream filtering bugs the engineering team had been quietly working around for weeks.
Working Across Different Models
Claude, ChatGPT, and Gemini all handle strict JSON instructions slightly differently, and it's worth knowing the quirks before you build a multi-model pipeline.
Claude tends to follow "no markdown fences" instructions reliably once stated explicitly, but if you don't state it, it defaults to wrapping JSON in a fenced code block for readability — which is actually helpful in a chat interface and actively unhelpful in an automated pipeline.
ChatGPT, especially in API mode with a JSON-mode or structured-output parameter enabled, can enforce schema compliance at the API level rather than relying purely on prompt instructions. If you're building anything at scale, check whether your provider offers a native structured-output mode before doing everything through prompt engineering alone — it's more reliable than prompt instructions by themselves.
Gemini generally respects explicit schema instructions well, but benefits from the schema being presented as a literal JSON example rather than a prose description — show it the shape you want rather than describing the shape you want.
⚡ Pro tip: whichever model you're using, test your JSON-extraction prompt against at least 10-15 varied real inputs before trusting it in production. A prompt that works perfectly on your first three test cases can still fail on the edge case where a field is genuinely empty, a number is written as a word ("three" instead of "3"), or the source text is in a slightly different format than what you tested.
Conclusion
Reliable JSON output comes down to treating the model like a strict API, not a conversational partner — explicit schema, explicit types, explicit "no extra text" instructions, and a repair step for the occasional slip. None of this is complicated once you've built it once, which is exactly why it's worth saving.
Once you land on a JSON extraction prompt that works for your specific use case, keep it somewhere reusable — a prompt library tool like PromptABCD lets you version these templates as your schema evolves, so when a field gets added six months later, you're editing one saved prompt instead of hunting through old Slack messages for the version that actually worked.
Continue Reading
Save the prompts from this post
PromptABCD is a free prompt manager. Paste, organize, and reuse your best AI prompts — no more hunting through chat history.
