Task 3.6
Output Formats & Structured CI Output
JSON output and schema enforcement in automated workflows
📘What You Need to Know
Why Structured Output Matters in CI
CI pipelines are machines, not humans. They cannot read a paragraph of prose and decide whether to block a merge. They need structured data — JSON with predictable fields — to make automated decisions. When Claude Code runs in CI with -p, the --output-format json flag transforms Claude's response into parseable JSON your scripts can consume.
Without structured output, teams resort to regex parsing of free text — fragile, error-prone, and guaranteed to break when Claude's formatting varies between runs. Structured output is the foundation of reliable CI automation.
--output-format json
The --output-format json flag tells Claude Code to return its response as JSON rather than plain text. This is the minimum requirement for CI integration. Your pipeline reads the JSON, extracts findings, and acts on them — posting PR comments, updating status checks, or failing the build.
A typical CI command combines three flags: -p for non-interactive mode, --output-format json for structured output, and a specific prompt that tells Claude what fields to include in its response.
claude -p "Review this PR for security issues. Return findings as JSON array with fields: severity, file, line, description." --output-format json > review-results.json--json-schema for Enforced Output Shape
JSON output alone does not guarantee consistent structure. Claude might return findings as an array one time and as a nested object another time. The --json-schema flag enforces a specific output shape — Claude's response must conform to the schema you provide. If it cannot, the call fails rather than returning malformed data.
This is critical for CI pipelines where downstream steps depend on specific field names and types. A schema with required fields [severity, file, line, message] and severity as an enum [critical, high, medium, low] ensures every response has the exact structure your pipeline expects.
claude -p "Review this code for security vulnerabilities" --output-format json --json-schema '{
"type": "array",
"items": {
"type": "object",
"required": ["severity", "file", "line", "message"],
"properties": {
"severity": {"type": "string", "enum": ["critical", "high", "medium", "low"]},
"file": {"type": "string"},
"line": {"type": "integer"},
"message": {"type": "string"}
}
}
}'--output-format json gives you JSON. --json-schema guarantees the JSON matches your exact structure.
CI Pipeline Integration Patterns
With schema-enforced JSON output, your CI pipeline follows a clean pattern: run Claude with -p, --output-format json, and --json-schema; parse the validated JSON; iterate over findings; post comments or set status checks; gate the merge if any critical findings exist. Each step is deterministic and testable.
Store the JSON output as a CI artifact for audit trails. When a developer asks "why did CI block my PR?", the artifact shows exactly what Claude flagged. Over time, tune your schema and prompts based on false positive patterns in the artifacts.
Schema Design Best Practices for CI
Design schemas with CI consumption in mind. Use required fields for data your pipeline depends on — if severity is required, your gate script never encounters a finding without a severity level. Use enum for categorical fields like severity to prevent unexpected values. Include a description field on each property for clarity.
Keep schemas focused on what the pipeline needs, not everything Claude could possibly say. A review schema needs severity, file, line, and message — not a full code rewrite. Smaller, focused schemas produce more reliable results than schemas trying to capture every possible output.
- •Use required for fields your pipeline depends on
- •Use enum for categorical fields (severity, category, status)
- •Keep schemas focused on pipeline needs, not exhaustive output
- •Store JSON artifacts for audit trails and prompt tuning
Parsing free-text Claude output with regex is fragile and an exam anti-pattern. Always use --output-format json with --json-schema.
⚠️Exam Traps
Trap: Parsing free-text Claude output in CI pipelines
Why it's wrong: Free-text formatting varies between runs; regex parsing breaks unpredictably.
Trap: Using --output-format json without --json-schema
Why it's wrong: JSON output alone does not guarantee consistent field names, types, or structure across runs.
Trap: Using --output-format markdown for CI automation
Why it's wrong: Markdown is designed for human reading, not machine parsing in automated pipelines.
Trap: Omitting the -p flag when using --output-format json in CI
Why it's wrong: Without -p, Claude Code runs interactively and the CI pipeline hangs waiting for input.
✓Concept Check
Your CI pipeline needs to automatically block PRs when Claude finds critical security vulnerabilities. Which flags should you use?
🔨Build Exercise
Intermediate40 minutesDefine a JSON Schema for CI Review Output
What you'll learn
- •Define a JSON schema for code review findings
- •Run Claude Code with schema-enforced output in CI
- •Build a pipeline gate that acts on validated JSON