⚡0 XP🔥0

Controlling Output Format (Tables, JSON, Markdown)

Lesson 7 of 19 ⏱ 4 min read ✎ 3 exercises ❓ 4-question quiz ▶ Runs Python

Get answers in exactly the shape you need - bullet points, tables, CSV for Excel, JSON for code, templates and fixed sections - and parse AI output safely in Python.

Why format matters#

The most common time-waster with AI is getting a correct answer in the wrong shape - a paragraph when you needed a table, a table when you needed code-readable data. Always say what shape you want.

Common formats and how to ask#

You need Ask for
Quick reading "exactly 5 bullet points, each under 15 words"
Comparison "a table with columns: Feature, Option A, Option B, Winner"
Excel / Google Sheets "CSV with header row: Name,Email,City - no extra text"
Program input "valid JSON only, matching this structure: ..."
Document "Markdown with H2 headings: Overview, Steps, Risks"
Fill-in template "use exactly this template: ..."
Short answer "answer with one word: Yes or No"
हिंदी में समझें

AI का जवाब सही हो, यह काफ़ी नहीं - वह सही रूप (format) में भी होना चाहिए, ताकि आप उसे सीधे इस्तेमाल कर सकें।

  • Excel में paste करना है? → CSV या table माँगें।
  • Python program में इस्तेमाल करना है? → JSON माँगें।
  • Report/notes चाहिए? → headings और bullet points माँगें।

जितना साफ़ आप format बताएंगे (columns के नाम, keys के नाम, कितने points), उतना कम दोबारा काम करना पड़ेगा।

Tables#

Compare Python, Java and JavaScript for a fresher choosing a first language.
Return a Markdown table with columns: Criteria | Python | Java | JavaScript.
Rows: Ease of learning, Job demand in India, Typical salary range for freshers,
Best used for, Learning time to job-ready.
After the table, give a 2-line recommendation.

CSV for Excel#

Extract every person mentioned in the text below.
Return ONLY CSV with this header: Name,Role,Company,City
Use "Unknown" for missing values. No explanation, no code fences.

<text>
...
</text>

Copy the reply into a .csv file or paste into Excel and use Data → Text to Columns.

JSON for programs#

Show the exact structure, with key names and value types:

Extract the order details from the WhatsApp message below.
Return only valid JSON in this exact structure:

{
  "customer_name": string,
  "items": [{"name": string, "quantity": integer}],
  "delivery_date": "YYYY-MM-DD" or null,
  "address": string or null
}

<message>
Hi, this is Meena. Please send 2 kg basmati rice and 3 packets of
Tata salt to 14 Saket Nagar by Saturday 29 Nov.
</message>

Expected reply:

{
  "customer_name": "Meena",
  "items": [
    {"name": "basmati rice (kg)", "quantity": 2},
    {"name": "Tata salt (packet)", "quantity": 3}
  ],
  "delivery_date": "2026-11-29",
  "address": "14 Saket Nagar"
}
Tip

Many AI APIs also offer a structured output / JSON mode setting that guarantees valid JSON matching a schema. When you build real applications, use it. In chat tools, the prompt above works well.

Parsing AI output safely in Python#

AI replies are text. Even when you ask for "JSON only", you might get code fences or a sentence around it. Write code that cleans and checks:

▶ Try it YourselfPython
Ctrl + Enter to run

Always validate - check required keys exist and values have sensible types before using them.

Templates - fill in the blanks#

Give a skeleton and ask the model to fill it:

Create a lesson plan using exactly this template:

TOPIC:
CLASS:
DURATION: 45 minutes
LEARNING OBJECTIVES: (3 bullets)
WARM-UP (5 min):
MAIN ACTIVITY (25 min):
PRACTICE (10 min):
HOMEWORK:

Topic: Python dictionaries. Class: B.Sc. first year.

Controlling style inside the format#

  • "Use bold only for key terms."
  • "No emojis."
  • "Write in prose paragraphs, not bullet points." (useful when AI over-uses bullets)
  • "Code blocks with language tags; comments in English."
  • "Numbers in Indian format (1,00,000)."

Prefill / start the answer#

In some tools and APIs you can start the assistant's reply yourself. Starting it with { strongly pushes the model to continue with JSON. In chat, a similar trick is ending your prompt with the first line of the output you expect:

...Return the table.

| Criteria | Python | Java |
|---|---|---|

Summary#

  • Always specify the output shape: bullets, table, CSV, JSON, Markdown sections or a template.
  • For programs, show the exact JSON structure and say "only JSON".
  • Parse AI output defensively: strip extra text, json.loads, validate required keys.
  • Use CSV for Excel; templates for consistent documents.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Extract JSON from an AI reply

AI replies sometimes wrap JSON in markdown fences or add text before/after it. Write extract_json(reply) that finds the text from the first { to the last } and returns it parsed with json.loads.

2

Validate required keys

Write missing_keys(data, required) that returns a list of keys from required that are not in the dict data (keep the order of required).

3

CSV reply to rows

An AI returned CSV text with a header row. Write csv_to_dicts(text) that returns a list of dictionaries (one per data row) using the csv module. Ignore blank lines.

Quick Quiz

Q1.

You want to paste AI output directly into Excel. Which format should you request?

Q2.

Why is JSON useful when calling AI from a program?

Q3.

Best way to get JSON with the exact keys you need?

Q4.

An AI wrapped its JSON in json ... markdown fences. What should your code do?