Module 3 · Talking to AI: prompting · Lesson 16 of 32
Getting clean, structured answers
10 minFree GenAI course · BeginnerLesson 3.4By Saket Bhatnagar
Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.
In short
- People can read a friendly paragraph. Apps need answers in a fixed shape, like a filled form.
- Ask for JSON with named fields, give the exact shape, and say what to do when something is missing.
- Always check the result before your app uses it: valid shape first, sensible values second.
Video lesson · coming soon
How to get clean JSON from AI
Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.
- 01 A lovely answer the app can’t read
- 02 Apps need a form, not a story
- 03 JSON: named fields and values
- 04 Messy messages become clean fields
- 05 Give the shape, then check it
- 06 Valid shape is not true facts
- 07 Try it, then fix common mistakes
Asha runs a kirana shop in Indore. Every morning her phone fills with orders: “Bhaiya 2 kilo atta, ek dozen ande aur ek sabun bhej do, address same as last time.” Her nephew Rohan, a second-year engineering student, decides to build a small app that turns these messages into an order sheet.
He asks an AI model, “What did this customer order?” The answer is lovely: “The customer would like two kilograms of wheat flour, a dozen eggs and a bar of soap!” His app, though, has no idea which part is the quantity. And the next answer is worded completely differently.
Why do apps need a fixed shape?
A person reading Rohan’s answer understands it at once. A program cannot. It needs to find the item name in the same place every time, the quantity as a number, and the unit as one of a few known words. A paragraph is a story. An app needs a form.
Meet JSON, the form apps understand
JSON stands for JavaScript Object Notation, but you don’t need JavaScript to use it. Here is Asha’s morning order as JSON. Read it slowly; it is more readable than it looks.
{
"items": [
{ "name": "atta", "quantity": 2, "unit": "kg" },
{ "name": "eggs", "quantity": 12, "unit": "piece" },
{ "name": "soap", "quantity": 1, "unit": "piece" }
],
"address": "same as last order",
"phone": null,
"needs_reply": false
}- Curly braces hold one record, like one filled form.
- Each line inside is a field: a name in double quotes, a colon, then the value.
- Square brackets hold a list, here the list of items.
- Numbers have no quotes, so 2 can be added and multiplied. Words sit in double quotes.
- true and false answer yes-or-no fields. null means “not given”, like an empty box left blank on purpose.
Notice that “ek dozen ande” became 12 pieces and “ek” became the number 1. Turning messy Hinglish into clean values is exactly the kind of work language models are good at. The cards below show more of these transformations.
How do you ask for JSON that works?
Rohan’s first try was “reply in JSON”. He got JSON, but the field names changed every time: “item”, then “product”, then “things_ordered”. The fix is to give the model the exact shape, the way the railway gives you the same form every time.
Name every field
Fix the exact field names, so “items” is always “items”.
Give each field a type
Say “quantity is a number”, so you never get the word “two”.
List allowed values
Unit must be one of kg, g, litre or piece. Fewer choices, fewer surprises.
Say what to do when it is missing
Use null if a field is not in the message. Never invent it.
Ask for JSON only
No greeting and no explanation, so your app does not trip over “Sure! Here you go.”
Keep the temperature low
This is not creative writing. A low temperature keeps the output steady from call to call.
Read the shop order message below and return JSON only, with no other text.
Use exactly this shape:
{
"items": [ { "name": string, "quantity": number, "unit": "kg" | "g" | "litre" | "piece" } ],
"address": string or null,
"phone": string or null,
"needs_reply": true or false
}
Rules:
- If a field is not in the message, use null. Never guess it.
- Convert words like "ek dozen" into numbers.
- Set needs_reply to true only if the customer asks a question.
Message: "Bhaiya 2 kilo atta, ek dozen ande aur ek sabun bhej do, address same as last time."If you are building an app, go one step further. Many AI APIs now offer a JSON mode or a structured output setting. You pass the schema itself, and the model is held to that shape while it writes. Use it when it is available. It removes most broken JSON before it happens.
From a messy message to a clean order sheet
Drag to exploreThe schema shapes the answer, and the validator checks it before anything reaches the order sheet.
Diagram connections:
- Customer message to Prompt + schema
- Prompt + schema to Model
- Model to Validator (JSON)
- Validator to Order sheet (passes)
- Validator to Model (retry once)
- Validator to Asha checks (still fails)
- Customer message: Free text, often in mixed languages.
- Prompt + schema: The instructions, the exact shape, and the rules for missin…
- Model: Writes the answer as JSON following the shape.
- Validator: Checks the JSON parses, every field exists and every value…
- Order sheet: Only answers that pass the check are saved.
- Asha checks: If a retry still fails, a person looks at the message.
Always check before you trust
Even a good prompt sometimes returns something your app cannot use. A program that blindly trusts the answer will crash, or worse, save nonsense. So every answer goes through a check first.
| Problem | Example | Fix |
|---|---|---|
| Extra words around the JSON | “Sure! Here is your JSON:” before the braces | Ask for JSON only, use JSON mode, strip text outside the braces |
| Broken JSON | A missing closing bracket or a stray comma | Use JSON mode; if it fails to parse, retry once with the error |
| Wrong type | "quantity": "two" instead of 2 | State the type in the schema and reject it in validation |
| Missing field | No "address" field at all | Require every field, with null allowed for unknowns |
| Made-up value | A phone number that was never in the message | Tell it to use null, and check values against the source text |
Check yourself
Check yourself
A customer’s message has no phone number. What should your prompt tell the model to do?
Pick an answer to see why it is right or wrong.
Check yourself
Rohan’s app receives valid JSON, but one item says "quantity": "two". What is the best fix?
Pick an answer to see why it is right or wrong.
Remember these three
- Apps need answers in a fixed shape; JSON with named fields is the standard form.
- Give the exact schema, types, allowed values and a null rule; use JSON mode when building.
- Validate every answer, retry or hand it to a person if it fails, and remember that the shape does not prove the facts.
Questions people ask
- What is structured output in AI?
- Structured output means the AI answers in a fixed, machine-readable shape, usually JSON with named fields, instead of a paragraph. It lets an app read the answer reliably, like reading the boxes of a filled form.
- How do I get JSON from an AI chat app?
- Ask for JSON only, paste the exact shape you want with field names and types, and say to use null for anything missing. If you are building with an AI API, turn on its JSON or structured output mode as well.
- Why does AI add text before or after the JSON?
- Chat models are trained to be conversational, so they often add a greeting or an explanation. Saying “return JSON only, no other text” helps, and a JSON mode in the API usually stops it completely.
- Is JSON mode always correct?
- It keeps the shape correct, not the content. The model can still misread a quantity or invent a value, so check answers in code and send unusual ones to a person.