Courses / Learn from zero
Get verifiable JSON from AI: prompt, schema, and rejection tests
Goal: ask an AI assistant for structured data and check both JSON syntax and a schema before using the result. Our entries are explicitly fictional books, not bibliographic facts. No paid API is required to practise the prompt and use a local JSON validator.
Teaching examples are not claimed live model runs; review each lesson’s limits.
Download practice worksheet ↓Lesson 1: Think of JSON as a labeled library card
A librarian can sort cards with fixed fields more easily than free-form blurbs. JSON uses key-value pairs, such as {"title":"Journey","year":2020}. A vague request for “three books” often yields prose. Instead ask AI: Make three fictional practice book records. Each has title as a string, year as an integer and available as a boolean. Return JSON only.
Illustrative single record: {"title":"Journey","year":2020,"available":true}. The sample does not identify an existing publication. Practice: compare "2020" and 2020. The quoted version is text; the unquoted one is a number. A casual reader may overlook this but a program cannot.
Review question: Why explicitly request JSON value types?
A program distinguishes strings, integers and booleans even when their printed forms look similar.
Lesson 2: Define a contract that restricts the shape
A model might helpfully add an author and price. If you did not request those, they should not silently enter the data. Prompt: Return an array of exactly three objects, with only title, year and available. No introduction, Markdown fences or extra fields. All records are fictional.
Try adding price:99 to a draft and ask AI to revise it to match the contract. The extra key must disappear. If available is a boolean, "yes" is not acceptable even though a human knows its meaning. Practice: state what each field represents before asking the model to produce many records. An AI instruction is not itself an enforcement mechanism; you will still validate the response.
Review question: What does additionalProperties:false enforce?
It rejects fields not included in the permitted object shape.
Lesson 3: Create a small JSON Schema
Describe the constraints to an independent validator. The outer type is an array whose items are objects. Each item requires title:string, year:integer and available:boolean, with additionalProperties:false. Ask: Write a JSON Schema for this list of fictional books. Include required and additionalProperties. Explain the rules in beginner language. Do not claim the schema confirms a book exists.
Practice: temporarily remove required. A record with no title may then pass; this reveals why “field exists” is different from “field has the correct type.” Check what schema features your chosen validator supports before relying on its result.
Review question: Does schema validation prove the books are real?
No, it proves structure and types, not external facts.
Lesson 4: Include deliberate failure cases
Compare two entries. Structurally valid: {"title":"Journey","year":2020,"available":true}. Invalid against our schema: {"title":"Journey","year":"2020","available":"yes"}. Prompt AI: Explain exactly which schema constraints the second entry violates, without overlooking that the strings are readable.
Then confirm its analysis with a real schema validator if available. Add an extra field while additionalProperties:false: that should fail too. Do not weaken the schema just so every generated result passes. Exercise: explain why a pretty-looking JSON response is not enough for a safe automation or import.
Review question: Why does year="2020" fail an integer field?
It is a quoted string instead of a numeric integer.
Lesson 5: Build a full AI-to-validation loop
Your workflow is: precise prompt, JSON response, parse check, schema check, error report, targeted correction request and second check. Ask: This record failed because year was a string. Change only year to an integer; leave every other value unchanged and return JSON only.
Final deliverable: three fictional sample records, a schema, an intentionally failing entry and its repaired version. Passing validation proves shape, not real-world truth: a fictional book can pass every type check. You now have a small but genuine AI structured-output workflow, not just a lesson about punctuation.
Review question: How should you fix a single failing field?
Identify the precise rule, correct only the field and revalidate without altering other values.
What did you accomplish?
Finish only when you can complete the final exercise and explain each step’s outcome or limits.
Topic-specific worked final project
Practice input
An AI assistant should produce three purely fictional books with title, year and available fields.
Reference deliverable
Prompt: “Return an array of three fictional books as JSON, title string, year integer, available boolean; no extra keys.”
Sample: {"title":"Journey","year":2020,"available":true}
Schema: array of objects, three fields required, no additional properties.
Intentional failure: {"title":"Journey","year":"2020","available":"yes"}.
Repair the wrong types, then run validation again.How do we judge success?
Working prompt, parsable example, clear JSON Schema and a rejected bad record; fictional data labeled honestly.