Intermediate · Shaping the Output
Practice - format a JSON output
JSON is the most common format for getting structured data out of Claude in a way that a downstream system can consume. The technique for getting reliable JSON is specific: you tell Claude the exact schema you want, you put the data inside delimiters, and you tell Claude to produce nothing else.
Why JSON output fails
The most common failure is asking for JSON without specifying the schema. "Extract the names and dates as JSON" produces different JSON every time — sometimes an array of objects, sometimes an object with arrays, sometimes something else. If your downstream system expects a specific structure, you have to specify it.
The second most common failure is Claude wrapping the JSON in an explanation. "Here is the JSON you requested: ..." followed by the JSON. If you are piping the output to a system, that prefix breaks the parse.
The pattern that works
Step 1: State the task. Step 2: Specify the schema. Step 3: Put the data in delimiters. Step 4: Tell Claude to produce only the JSON, no explanation.
Assembled:
Extract the people and dates mentioned in the following paragraph.
Return your answer as JSON with this exact structure:
{"people": [{"name": "string", "date": "string", "context": "string"}]}
If no people or dates are found, return {"people": []}.
Produce only the JSON. No explanation, no introduction.
<paragraph>
Sarah Chen joined the project on April 3rd. Marcus Webb had been leading the infrastructure work since January and was scheduled to hand off to the new team by May 15th.
</paragraph>
Expected output:
{"people": [{"name": "Sarah Chen", "date": "April 3rd", "context": "joined the project"}, {"name": "Marcus Webb", "date": "January", "context": "leading infrastructure work"}, {"name": "Marcus Webb", "date": "May 15th", "context": "scheduled handoff"}]}
Handling edge cases in the schema
Always specify what Claude should return when the data is empty or ambiguous. "If no people or dates are found, return {"people": []}." Without this, Claude may return null, an empty string, or an explanation of why there was nothing to extract — none of which parse correctly.
If you get stuck
If Claude is returning JSON with a different structure than you specified, check that your schema example is valid JSON. An extra comma or a missing bracket causes Claude to produce what it inferred you meant, not what you wrote.
If Claude keeps adding an explanation before the JSON, add "Your entire response must be valid JSON. Begin your response with {." This constraint is usually sufficient.
Cross-reference
L7 (Separating data from instructions) covers the delimiter technique used here. L13 (Building complex prompts from scratch) shows how to combine schema specification, delimiters, and guardrails into a production-grade prompt.
Deliverable
Write a prompt that extracts structured data from a short paragraph of your choice. Define the schema explicitly. Run it. Paste the output into a JSON validator (jsonlint.com or similar). If it passes validation, the prompt works. If it does not, debug the schema specification and try again.
Try these prompts
- Extract the names and dates from this paragraph.
- List the top 3 risks in the project update.
- Summarize the customer feedback as JSON with sentiment scores.
Sign in to keep your place and tick off lessons. Reading stays open either way.