Skip to content
Getting Started Intermediate

Use Structured Output (JSON Schema)

Force Claude to return data matching an exact JSON schema via constrained decoding

Command

$ "color:#7C5CFC">claude -p "Extract: 'John Smith is 32 years old'" \
    "color:#d97757">--output-format json \
    "color:#d97757">--json-schema '{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name","age"]}'

Response

{
  "structured_output": {
    "name": "John Smith",
    "age": 32
  },
  "result": "",
  "total_cost_usd": 0.073
}

Parsing Code

059669">">const data = JSON.parse(output);
059669">">// Always read structured_output, NOT result
059669">">const { name, age } = data.structured_output;
console.log(059669">`${name} is ${age} years old`);

Gotchas

! Requires --output-format json — text format produces empty output with --json-schema
! Read structured_output field, not result — they are independent

Related Recipes