Lesson 16 of 1570
Structured Output: JSON and XML
When your prompt feeds into code, you need machine-readable output. JSON mode and XML tags make the AI's response parseable instead of loose prose.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1Why structure matters
- 2JSON mode
- 3XML tags
- 4structured output
Concept cluster
Terms to connect while reading
Section 1
Why structure matters
If you're using an AI inside an app, you don't want a friendly paragraph — you want data. A list of book titles. A calendar event with a date and duration. A code file and a test file. Structured output is how you get clean, predictable data the rest of your program can use.
JSON output
A typical JSON extraction prompt.
Analyze this customer review and return ONLY valid JSON matching this schema:
{
"sentiment": "positive" | "neutral" | "negative",
"topics": string[],
"urgent": boolean,
"summary": string (max 20 words)
}
Do not include any text before or after the JSON.
Review: "Bought this for my grandma. The setup was a nightmare — the instructions are in tiny print and missing page 3. But once my son helped her get it working, she loves it. Still, I'd return it if I could."The AI will return a JSON object like {"sentiment": "negative", "topics": ["setup", "instructions", "accessibility"], "urgent": false, "summary": "Setup was frustrating but grandma loves the product."}. Your code can JSON.parse it and move on.
XML tags (Claude-style)
Anthropic's Claude was trained with lots of XML-tagged data, so it responds very well to XML tags for structure. You can both send and request them.
XML tags separate input from output and make fields easy to parse.
I'll give you a support email. Extract information and wrap your answer in XML tags.
<email>
Subject: Broken laptop
Hi, my laptop won't turn on. I ordered it 3 weeks ago, order #A8823. I'm out of town until Friday. - Mike
</email>
Respond with:
<customer_name>...</customer_name>
<order_id>...</order_id>
<issue>...</issue>
<urgency>low | medium | high</urgency>
<suggested_reply>...</suggested_reply>When to pick which
Compare the options
| JSON | XML Tags |
|---|---|
| Best for data going into code. | Best for long content with multiple sections. |
| Compact, widely supported. | More readable for humans, flexible for mixed content. |
| Brittle — one stray comma breaks it. | Forgiving — ignores stray text outside tags. |
| Ideal: extractions, classifications, small objects. | Ideal: essays with sections, multi-step reasoning, document analysis. |
Real extraction task — feeds right into a calendar app.
You are a calendar assistant. Extract events and return ONLY JSON like:
[
{
"title": string,
"date": "YYYY-MM-DD",
"start_time": "HH:MM",
"duration_minutes": number,
"participants": string[]
}
]
Text:
"Team sync is Wednesday at 10am, 30 minutes, with Maya and Dev. Then I have yoga Thursday 6pm for an hour."Key terms in this lesson
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Structured Output: JSON and XML”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 28 min
The Five-Part Prompt: Role, Context, Examples, Constraints, Format
Pro prompters follow a structure. Give the AI a role, set the context, show examples, set constraints, and pick a format. This framework alone 10x's your output quality.
Builders · 22 min
Iterate, Don't Rewrite
Beginners scrap their prompt and start over. Pros keep the good parts and change only what isn't working. Here's how to iterate like a craftsperson.
Builders · 24 min
Prompt Templates: Write Once, Use Forever
Turn your best prompts into reusable templates with variables. This is how pros scale: one great template, thousands of runs.
