CC-301l · Module 1

Output Formatting and Parsing

3 min read

When Claude Code outputs to stdout in prompt mode, the output is unstructured text by default. For automated workflows, you need structured output that downstream tools can parse. The --output-format json flag produces JSON output with metadata: the response text, token usage, model used, and session ID. This is the format you want for programmatic pipelines where the output feeds into another system.

For simpler use cases, instruct Claude to produce structured output in the prompt itself: "Output the result as a JSON object with fields: summary, issues, and recommendations." Claude follows formatting instructions reliably in prompt mode because there is no conversational context to interfere — the prompt is the complete instruction. Combine the --output-format json flag for pipeline metadata with in-prompt formatting instructions for the content structure.

# JSON metadata output
claude -p "Count TypeScript errors in this project" \
  --output-format json | jq '.result'

# Structured content via prompt instruction
claude -p "Analyze src/auth/validate.ts. Output as JSON:
{
  \"functions\": [{\"name\": \"\"  , \"params\": [], \"returns\": \"\"}],
  \"complexity\": \"low|medium|high\",
  \"issues\": []
}" --output-format json

# Stream output for long operations
claude -p "Refactor the entire auth module" --stream