Pull Data Out as JSON
Part of the Prompting Techniques section of Coddy's AI Prompts journey. Lesson 12 of 24.
Tables are for people. When a program reads the answer, you want JSON: data written as keys and values in curly braces, like {"city": "Oslo", "temp": 12}. Code can read it directly, with nobody retyping.
Extract the name and age from this sentence: "Maria is 34 and lives in Lisbon." Return only a JSON object with the keys "name" and "age".{"name": "Maria", "age": 34}
Two things make it dependable. Name the exact keys, so the program finds "age" and not "Age" or "years". And say return only, because a friendly sentence before the braces breaks the program reading it. Some models also wrap JSON in a code block, which a program has to strip first, so add no code block when the reply goes straight into code.
Challenge
MediumYour shop's order system needs this order as JSON:
3 blue mugs shipped to Porto
Use the keys item, quantity and city.
The AI's response must:
- Be a JSON object
- Fill item and city, with quantity 3
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Prompting Techniques
3Structured Output
Tables From TextPull Data Out as JSONWhen Data Is MissingFill a TemplateRecap: Structured Output