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.
Flat schemas are easy; reliability gets hard at the first nested array. How to contract arrays and objects so the structure survives the model.
Most JSON contract failures hide in the nesting: the array that comes back as a comma-joined string, the object that flattens into prefixed keys, the list of objects where item shapes drift between elements. Contracting nested output means showing it: the example response must demonstrate the exact nesting, and field descriptions must define what array items and object properties contain. This resource loads a user-profile schema with both an array field and an object field — the smallest schema that exercises every nesting rule.
Generate and study the example
The example demonstrates the array AND the object nesting explicitly — the model imitates what it sees.
Define items in descriptions
'Topic interests, lowercase tags' defines array items; 'with street and city' defines object properties. Descriptions are the nested schema.
Watch the null rule on nested fields
An unknown address is null — not an empty object, not omitted. One convention, applied at every depth.
The contract fights that collapse in two spots. The example shows interests as a real bracketed list (['item-one', 'item-two']), and the schema labels it 'Topic interests, lowercase tags', so the model reads items as array elements, not one string. The JSON Output Prompt Builder writes both signals into the prompt; deep nesting still slips sometimes, so parse the array after running the prompt in ChatGPT, Claude, or Gemini.
Flattening shows up when a prompt names sub-fields but never demonstrates the nesting. This one prevents it by rendering address as a genuine nested object in the example, street and city as inner keys, plus the label 'Postal address with street and city'. The model imitates that shape. The builder bakes the signal into the prompt; confirm the nested path survived, since keys can still get hoisted up.
Null, with the key still present. One convention runs at every depth: optional fields with no value are set to null and never omitted, so an unknown address returns as "address": null, not {} or a dropped key. That uniformity lets your deserializer treat nested absence like a missing top-level field. The builder encodes the rule; still verify the object case, where an empty {} is the common slip.
Two levels is the practical ceiling this schema was built to exercise: a top object holding one flat array (interests) plus one one-deep sub-object (address, with street and city). Push a deeper recursive tree through a single prompt and reliability falls off; splitting generation across calls holds up better. The builder still emits a contract for deeper shapes, so validate the deepest paths hardest wherever you run it.
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.