Use --output-format json to Get Structured Output for Scripts and Automation
Claude Code's default output is human-friendly text, but when you're piping results into another script or building an automated pipeline, you need something a machine can parse. That's what --output-format json is for.
# Get structured JSON output from a one-shot command
claude -p --output-format json "list all TODO comments in the codebase"
The output includes structured data about every step Claude took — the messages, tool calls, file reads, and the final response — all as parseable JSON. Your scripts can extract exactly the fields they need.
This pairs naturally with -p for automation workflows:
# Extract just Claude's answer using jq
claude -p --output-format json "what's the primary database driver?" \
| jq -r '.[-1].content'
# Build a script that processes Claude's structured output
claude -p --output-format json "find all deprecated functions" \
| jq '[.[] | select(.type == "assistant")] | last'
You can also use --output-format stream-json for streaming JSON — each line is a separate JSON object emitted as Claude works, so your script can process results in real time instead of waiting for the full response:
# Stream JSON events as they happen
claude -p --output-format stream-json "refactor the auth module" \
| while read -r line; do
echo "$line" | jq -r '.type'
done
Practical automation use cases:
- CI pipelines — parse Claude's code review into structured findings
- Custom dashboards — extract metrics from Claude's analysis
- Chained tools — feed Claude's JSON output into other CLI tools
- Logging — capture structured records of what Claude did and why
Plain text is for humans, JSON is for machines — use --output-format json when your script is the one reading.
via Claude Code
Log in to leave a comment.
When Claude writes error messages, button labels, validation text, or onboarding flows, it defaults to generic developer-speak. Add a "Users" section to your CLAUDE.md describing who your actual users are — their technical level, industry jargon, and what they care about — so Claude writes copy that makes sense to THEM, not to developers.
Use the --agent flag with custom markdown files in .claude/agents/ to launch purpose-built Claude sessions with restricted tools and scoped system prompts.
Every project has traps — the billing module that silently fails if you forget to queue the job, the legacy table with column names that don't match the model, the config value that must be set before tests run. Document these gotchas in your CLAUDE.md so Claude avoids the same mistakes your team spent days debugging.