The moment an AI answer is read by a program rather than a person, its format stops being a matter of taste. A spreadsheet import, an automation or an app expects data in an exact shape, and a model that adds a friendly sentence before the data, renames a field or leaves one out breaks the next step.
JSON is the usual answer, and there are three ways to get it, each more reliable than the last: ask for it in the prompt, switch on a JSON mode, or give the model a schema it must follow. This post explains all three and when each is enough.
Level 1: ask for JSON in the prompt
The simplest approach works in any chat app or API: describe the JSON you want, and give an example.
Extract the invoice details from the text below. Reply with JSON only, with no explanation before or after it. Use exactly these fields: invoice_number (text), issued (date as YYYY-MM-DD), client (text), total (number, no currency symbol), lines (a list, each with description, qty and unit_price).
This works most of the time with current models, and “most of the time” is the problem. Over hundreds of requests, some answers will arrive wrapped in a code block, with a sentence of explanation, with a field renamed, or with a number written as text. Prompt-only JSON is fine for one-off tasks and prototypes. For anything automated, plan to validate every answer.
Ways to make prompt-only JSON more reliable:
- List every field with its type, and say what to do when a value is missing, such as using null.
- Give one complete example of the output you expect.
- Say “JSON only” and that the reply will be read by a program.
- Keep field names simple: lowercase, no spaces, the same in every request.
The AI prompt generator has a JSON-only format option and a place for rules such as these.
Level 2: JSON mode
Many APIs offer a setting that guarantees the reply is valid JSON: it will parse without errors. That removes the code blocks and the stray sentences. It does not guarantee the right shape. A model in JSON mode can still leave out a field, add one you did not ask for or choose a different name. You still need the field list in the prompt, and you still need to check the result.
JSON mode is a good middle step when you need parseable output but the exact shape can vary a little, for example when you will look for a few fields and ignore the rest.
Level 3: structured output with a schema
The most reliable approach is to give the model a JSON Schema: a precise description of the fields, their types and which are required. Providers that support schema-enforced output, often called structured outputs, constrain the model so that its reply matches the schema. Microsoft’s documentation for structured outputs in Azure OpenAI describes the feature and its rules.
Writing a schema by hand is tedious. The JSON Schema generator builds one from a sample: paste an example of the JSON you want, and it infers the types, properties, required fields and array items.
The rules of strict mode
Schema-enforced output usually supports only part of JSON Schema. Microsoft’s documentation lists these rules for strict structured outputs:
| Rule | What it means for your schema |
|---|---|
| All fields must be required | Every property appears in the required list |
| additionalProperties must be false | No object can contain fields the schema does not name |
| Optional values use a union with null | A field that can be empty is typed, for example, string or null |
| Some keywords are not supported | Such as format, pattern, minimum and maxLength |
| Size limits | Up to 100 object properties in total and five levels of nesting |
The strict option in the generator applies these rules. Check your own provider’s current documentation before relying on them, because providers differ and the rules change.
An invoice schema, worked through
Here is the sample a business might paste into the generator for invoice extraction:
| Field | Sample value | Schema type |
|---|---|---|
| invoice_number | INV-1042 | string |
| issued | 2026-11-14 | string |
| client.name | Harbour Café | string |
| lines[].qty | 40 | integer |
| lines[].unit_price | 4.2 | number |
| notes | null | must be edited |
Two things need a human decision. The notes field is null in the sample, so its type is unknown; the generator says so, and you replace it with a string-or-null type. And qty is typed integer because the sample held whole numbers, which is right for quantities but would be wrong for a price that happened to be 12 in the sample.
Describe each field in words
A schema says what type a field is, not what belongs in it. A field called total typed as a number could hold the total before tax, after tax or after a discount, and the model has to guess which. Say it in words: in the prompt, in a list of fields with one line each, and if your provider passes them to the model, in a description for each property in the schema. “total: the amount payable including tax, as a number with no currency symbol” leaves nothing to guess.
The same goes for missing values. Decide what the model should do when the source text does not contain a field: use null, use an empty string or leave the item out. Say it once, clearly, and the answers will be consistent.
Use fixed lists for fixed choices
When a field can only take a few values, such as a status of draft, sent or paid, list them. JSON Schema has an enum keyword for exactly this, and it is among the features strict structured outputs support. A fixed list stops the model inventing near-synonyms such as “issued” or “awaiting payment”, which would otherwise break any code that checks for the exact word. Add enums by hand after generating a schema, because a single sample cannot show every value a field might take.
Test on real examples before you automate
Before an extraction runs unattended, try it on twenty to fifty real inputs and compare every answer with what a person would have entered. Include the awkward cases deliberately: a document with a missing field, an unusual date format, a different currency, a scanned page with errors in the text, a document in another language. The mistakes you find in an afternoon of testing are far cheaper than the ones you find in your records a month later.
Validate everything anyway
Even schema-enforced output deserves a check before it reaches anything important. The schema guarantees the shape, not the truth: a model can put the wrong number in the right field just as easily as in the wrong one. A sensible pipeline:
- Parse the reply as JSON.
- Validate it against the same schema with a JSON Schema validator library.
- Check the values that matter, such as totals that should equal the sum of the lines.
- Handle failures by retrying once, then sending the item to a person.
Models can also decline a request, for example when the input is not what the prompt described. Make sure your code expects a refusal or an error as well as a successful reply.
Choosing the right level
| Situation | Use |
|---|---|
| A one-off task in a chat app | A careful prompt with an example |
| A prototype or a small script | A prompt plus JSON mode, with validation |
| An automation that runs unattended | A schema with structured output, with validation |
| Data that feeds money, records or customers | A schema, validation and a human check on failures |
Keeping schemas small
Every field in a schema is something the model must produce, and the schema itself counts towards the input. Ask only for the fields you will use. A schema of a few dozen properties is easy to work with; one of hundreds is harder for the model and for you. The token counter estimates how much of the input a long schema and its instructions take up.
Good field names help as much as good structure. A field called unit_price_excluding_tax tells the model exactly what to put there; a field called amount leaves it guessing.
When JSON is the wrong format
JSON is for programs. If a person will read the result, ask for prose, a list or a table instead: JSON is hard to scan and easy to misread. If the result is going straight into a spreadsheet, a simple table or comma-separated rows may be all you need. And for long passages of text, such as a drafted email inside a JSON field, remember that quotation marks and line breaks inside the text have to be escaped, which is one more thing that can go wrong. Keep long free text out of JSON where you can, or validate those fields with particular care.
Mistakes that break JSON pipelines
- Asking for JSON with no field list. The model invents a structure, and a different one next time.
- Trusting JSON mode to enforce the shape. It guarantees valid JSON, not your JSON.
- Building the schema from a sample with gaps. Empty arrays and null values leave types unknown.
- Skipping validation because “structured output never fails”. Shape and correctness are different things.
- Putting instructions only in the schema. Descriptions of what each field means still belong in the prompt.
For more on what a model can take in at once when the input is a long document, see what is a context window.
Sources, read November 14, 2026:
- Microsoft Learn, How to use structured outputs with Azure OpenAI: all fields required, additionalProperties false, optional values as a union with null, unsupported keywords, and the limits of 100 object properties and five levels of nesting.