Get structured JSON output for scripts
cli flags & headless
// what it does
claude -p "query" --output-format json returns the result plus session metadata, including total_cost_usd and a per-model cost breakdown, so scripts can track spend and parse the text from the .result field with jq. Add --json-schema '{...}' to force the answer to match a JSON Schema; the validated data lands in the .structured_output field.
// shell
$ claude -p "summarize this project" --output-format json | jq -r '.result'$ claude -p "extract function names from auth.py" --output-format json --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}'// gotcha
If the value passed to --json-schema is not a valid JSON Schema, claude exits with "Error: --json-schema is not a valid JSON Schema" and the validator's diagnostic. The format keyword (like "format":"email") is accepted but treated as an annotation, not enforced.