Structured Outputs & JSON 7 min read Updated Jul 7, 2026

How to Make AI Return Valid JSON 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.

Try the JSON Output Prompt Builder

Why AI-generated JSON breaks

You ask a model for JSON, wire the response into your code, and it works — until the day it doesn't. The model wraps the JSON in a markdown code fence, adds a friendly "Here's your data:" line before it, trails a comment after the closing brace, or quietly swaps a number for the string "N/A" when a value is missing. Your parser throws, and the whole call fails.

No prompt can guarantee valid JSON on every run, but a clear schema, a missing-data rule, a no-markdown instruction, and a validation step make parseable output much more reliable. The fix is not a magic phrase. It's structure: tell the model exactly what shape you want, keep your instructions separate from that shape, show one correct example, and decide up front what happens when data is missing. Then validate the output before you rely on it. This guide walks through each step, and the tools that help you build the prompt — you still run the prompt in your own AI assistant and check the result yourself.

What "valid JSON" actually means here

Two things have to be true. First, the text has to be syntactically valid JSON — no code fences, no prose around it, no trailing commas, correctly quoted keys and strings. Second, it has to match the shape your code expects: the right keys, the right types, and a predictable answer when a field has no value.

A response can be perfect JSON and still be useless to you if "price" comes back as "129.99" (a string) one time and 129.99 (a number) the next. Reliable output means both the syntax and the schema are stable across runs.

Why prompts produce invalid JSON

Most invalid JSON traces back to one of a few habits in the prompt itself:

  • The prompt asks for JSON but never shows the exact shape, so the model invents its own key names and nesting.
  • Instructions and schema are mixed into one paragraph, so the model treats part of the schema as prose to explain rather than a contract to follow.
  • No example is given, so the model guesses at formatting decisions like date style or how to represent an empty list.
  • The prompt never says what to do when a value is missing, so the model improvises with "N/A", "unknown", or an omitted key.
  • The prompt allows conversational framing ("Sure! Here is the JSON:"), which the model then adds around the object.

Step 1: Define the exact JSON shape

Write the shape out explicitly — every key, its type, and whether it can be null. Don't describe it in a sentence; show it. Types like string, number, boolean, and array remove the guesswork about how each field should be represented.

The JSON Output Prompt Builder exists for exactly this step: you give it the fields you want and it produces a prompt that states the shape as a strict contract. If you already have a JSON Schema, the Turn a JSON Schema into a Prompt resource turns it into prompt language you can paste in.

The shape you specify
{
  "title": string,
  "price": number,
  "in_stock": boolean,
  "tags": string[],
  "discount_percent": number | null
}

Step 2: Separate the instructions from the schema

Keep two blocks distinct: what the task is ("extract the product details from the text below") and what the output must look like (the shape from Step 1). When they're jammed together, the model can misread a schema line as something to explain in prose.

A clear structure is: a short task line, then a labelled OUTPUT block containing only the shape and the output rules — "Return only the JSON object. No markdown, no code fences, no text before or after." That last rule is what stops the friendly wrapper text.

Step 3: Give exactly one valid example

One concrete example resolves the formatting questions a schema alone leaves open: how dates look, how an empty array is written, how a decimal is formatted. Show a single filled-in object that obeys the shape precisely.

One is enough. Several examples add length and can pull the model toward copying your sample values instead of using the real input.

Step 4: Say what to do when data is missing

This is the step most prompts skip, and it's the one that breaks parsers. Decide the rule and state it plainly: use null for a missing value, keep every key even when empty, and never invent a value that isn't in the source. Without that instruction, the same prompt will drop a key one run and fill it with "unknown" the next.

If your downstream code expects a fixed set of keys, spell that out too: "Always include all keys. Use null when a value is absent." The Force JSON Output from AI resource collects wording like this you can reuse.

Step 5: Validate the output before you use it

Even a well-structured prompt can produce a bad response, so treat validation as a required step, not an optional one. Parse the JSON in code and check it against your schema before it touches anything that matters. If parsing fails, you catch it at the boundary instead of three systems downstream.

As a review step while you're refining the prompt, the AI Output Validator helps you inspect a sample response and see where it drifts from the shape you asked for. It surfaces issues for you to judge — it doesn't run the model or approve the output on your behalf, so the final check stays with you and your own code.

Common mistakes

A few patterns cause most of the failures:

  • Leaving code fences allowed. If you don't explicitly forbid markdown, many models wrap the object in triple backticks. The resource on how to stop AI wrapping JSON in markdown fences covers the exact phrasing.
  • Trusting the first success. A prompt that returned clean JSON three times can still fail on an edge-case input; validate every response, not just the first.
  • Over-nesting. Deeply nested objects are harder for the model to keep consistent — flatten where you reasonably can.
  • Mixing types for the same field. Pin numbers as numbers and booleans as booleans in the shape, or you'll get strings some of the time.
  • Asking for extra commentary "as well as" the JSON. Anything outside the object risks landing inside the response your parser reads.

A worked example

Suppose you're extracting product data from a messy description. Your task line is "Extract the product details from the text between the delimiters." Your OUTPUT block states the shape from Step 1, forbids markdown, and says to use null for anything missing. Run that in your assistant and a good response looks like this — nothing before it, nothing after it:

A response that parses cleanly
{
  "title": "Wireless Headphones",
  "price": 129.99,
  "in_stock": true,
  "tags": ["audio", "bluetooth"],
  "discount_percent": null
}

Where this fits

If reliable JSON is a recurring need rather than a one-off, the AI JSON Output Workflow connects these steps into a repeatable process alongside the tools above. And when JSON extraction is one stage of a bigger build — say, pulling structured records out of documents and moving them somewhere — the Build a Data Pipeline with AI project shows where that stage sits in the whole path. Whichever route fits, NewPrompt's part is the prompt and the process — you run it in your own AI assistant, validate the output, and decide what happens next.

Tools for this guide

Each generates the prompt described above — you run it in your own AI assistant.

Ready-made resources

Reusable prompts and templates for the exact steps in this guide.

Take it further

When this task is one step inside a larger workflow or build.

FAQ

Can a prompt guarantee the AI always returns valid JSON?

No prompt can guarantee it, which is why validation is a step in this guide rather than an afterthought. A well-structured prompt — explicit shape, separated instructions, one example, a missing-data rule, and a no-markdown instruction — makes valid JSON far more consistent, but you still parse and check each response in your own code before relying on it.

Does the JSON Output Prompt Builder validate the AI's response?

It doesn't. The JSON Output Prompt Builder helps you write a stricter prompt that asks for the exact shape; it produces prompt text, not a running validator. You run that prompt in your own AI assistant, and you validate the output — parsing it in code, or reviewing a sample with the AI Output Validator while you refine the prompt.

Should I use JSON mode or a JSON schema if my model supports it?

Use it when available — a native JSON mode or schema constraint reduces syntax errors at the source. It still helps to specify the exact shape, give an example, and define missing-data behavior, because JSON mode guarantees valid syntax but not the keys, types, or null handling your code depends on.