LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 513 min read

Structured Output: Getting JSON, Tables & XML from AI

Learn to ask an AI for JSON, tables and XML-tagged sections, how to describe a schema, and how to keep structured output valid enough for a program to use.

On this page 8 sections

Introduction

So far the answers you have asked for were meant for people to read. But AI is increasingly used inside software: extracting details from invoices, tagging support tickets, turning notes into calendar events. For that, the answer must have a predictable structure a program can read. This lesson shows how to ask for structured output — and how to stop it from breaking.

What You Will Learn
  • How to describe a JSON format the model should follow.
  • When a table is the right structure for human readers.
  • How XML-style tags separate your data from your instructions.
  • How to make structured output reliable enough to use in code.

Why Structure Matters

A friendly sentence like “The customer, Priya, wants a refund of ₹1,200” is easy for a person to read but awkward for a program, which would have to guess where the name and the amount are. A structured answer puts each piece of information in a named field, so code can read it directly and reliably, thousands of times a day.

Asking for JSON

JSON is the most common format for structured output. To get it reliably, describe the exact fields, their types and what to do when information is missing — ideally with a template the model can copy.

Prompt
Extract the order details from the customer message below.
Reply with JSON only, no other text, using exactly this shape:
{
"customer_name": string,
"product": string,
"quantity": number,
"delivery_date": string in YYYY-MM-DD format, or null if not given
}
Message: "Hi, this is Ravi. Please send 3 packs of A4 paper."
Expected reply
{
"customer_name": "Ravi",
"product": "A4 paper",
"quantity": 3,
"delivery_date": null
}

Three details make this prompt robust: “JSON only, no other text” (so there is no friendly sentence before the JSON), a type for every field (so 3 is a number, not the string “three”), and an explicit rule for missing information (null) so the model does not invent a delivery date.

Mini Challenge · Predict the Output

Your program passes the model’s reply straight to a JSON parser. Which reply can be parsed without an error?

Choose one answer

Tables for People

When a person will compare several items across the same attributes, a table is easier to scan than paragraphs. Name the columns you want and their order: “Compare these three laptops in a table with columns: Model, Price, Battery life, Weight, Best for.” Many chat apps display markdown tables neatly, and you can paste them into documents or spreadsheets.

XML Tags for Inputs and Outputs

XML-style tags are simple labels like <article> and </article> wrapped around a section of text. They solve two problems. First, they make it unambiguous where pasted material starts and ends, so the model does not confuse the content with your instructions. Second, you can ask for answers in named sections, which makes them easy to find or extract.

Prompt using tags
Read the job advert in the <advert> tags. Then reply with:
<skills> a bulleted list of required skills </skills>
<summary> a one-sentence summary of the role </summary>
<advert>
We are hiring a junior web developer with HTML, CSS and
JavaScript skills. Experience with React is a plus...
</advert>

The tag names do not need to be special words — <advert>, <notes> or <draft> all work, as long as you use them consistently and refer to them by name in your instructions.

Making It Reliable

  • Validate in code: always check that the reply parses and has the expected fields before using it, and retry or report an error if not.
  • Use built-in structured output features when available: many AI APIs can enforce a JSON schema, which guarantees valid output.
  • Give one example of the exact output, as you learned in Lesson 4.
  • Keep schemas small and flat at first; deeply nested structures are harder to get right.
  • Say what to do with missing or uncertain values so the model does not guess.
Mini Challenge · Spot What's Missing

This prompt sometimes returns a made-up phone number when the email has none. What is it missing?

Prompt
Extract the sender's name, email address and phone number from the email below as JSON with the keys "name", "email" and "phone".
Choose one answer

Key Takeaways

  • Structured output lets programs use AI answers reliably.
  • For JSON: say “JSON only”, list every field with its type, and define what to do with missing values.
  • Tables help people compare; XML-style tags separate data from instructions and label output sections.
  • Always validate structured output in code, or use an API feature that enforces a schema.

Summary

You can now ask for answers in shapes that both people and programs can rely on. Next you will tackle harder problems by asking the model to reason step by step and by splitting big tasks into smaller prompts.

Next Lesson →

Step-by-Step Reasoning: Breaking Big Tasks Down