tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Anthropic Claude API > Claude Structured JSON Output

Claude Structured JSON Output

Author: Venkata Sudhakar

For data extraction and classification tasks you need consistent machine-readable JSON, not conversational prose. Claude does not have a strict JSON mode like some other APIs, but with the right prompting pattern it produces clean, parseable JSON every time. The key is a system prompt that explicitly instructs Claude to return only JSON with no other text, a clear schema definition in the prompt, and asking Claude to think step by step in a scratchpad field before populating the actual data fields. This pattern reliably produces valid JSON even for complex multi-field extractions from messy unstructured input.

The most reliable prompting pattern has three elements: tell Claude exactly what JSON keys to return and what each means, instruct it to reply with only JSON and nothing else including no markdown code fences, and use a "reasoning" field as the first key so Claude reasons through the extraction before committing to values. This reasoning field acts as chain-of-thought that improves accuracy on ambiguous fields. Strip the reasoning field before writing to your database. For very high reliability on complex schemas, combine this pattern with the Batch API for overnight processing of large document sets.

The below example shows a procurement system extracting structured data from unstructured purchase order emails - handling inconsistent formatting, missing fields, and ambiguous values reliably across different supplier communication styles.


Extracting from three different PO email formats,


It gives the following output with consistent structured data across all three email formats,

--- PO Email 1 ---
Supplier:     Bharat Engineering Works
PO Number:    PO-2025-0412
Order Date:   2025-04-01
Delivery By:  2025-04-10
Total:        Rs None
Payment:      30 days from invoice
Items:
  - 50 kg MS Flat Bar 50x6mm @ Rs 185 per unit
  - 30 kg MS Angle 50x50x6mm @ Rs 175 per unit

--- PO Email 2 ---
Supplier:     Nexgen Solutions
PO Number:    None
Order Date:   None
Delivery By:  None
Total:        Rs None
Payment:      None
Items:
  - 200 units A4 Copy Paper 80gsm @ Rs None per unit
  - 50 units Stapler Pins (box of 5000) @ Rs None per unit

--- PO Email 3 ---
Supplier:     Trident Chemicals Pvt Ltd
PO Number:    NCB/2025/418
Order Date:   None
Delivery By:  None
Total:        Rs 39500
Payment:      Advance RTGS before dispatch
Items:
  - 100 litres Isopropyl Alcohol 99.9% @ Rs 320 per unit
  - 500 litres Distilled Water @ Rs 15 per unit

# Consistent schema across three completely different email styles
# Missing fields return null - safe to INSERT into your DB with null handling
# No JSON parse errors - ready for result.dict() and direct DB write

For maximum JSON reliability in production: validate the output with Pydantic after json.loads() so type mismatches are caught immediately. Use a retry wrapper - if json.loads() raises JSONDecodeError, send the malformed output back to Claude with the instruction "Fix this malformed JSON and return only the corrected JSON". This two-attempt pattern handles nearly all edge cases. For documents with very complex or deeply nested structures, split extraction into two passes: extract the header fields first, then loop over line items separately - this reduces the chance of any single call producing a truncated JSON object.


 
  


  
bl  br