已验证 Claude Code
用 --json-schema 让输出严格符合你的格式
做什么
给 -p 调用传一个 JSON Schema,结果会出现在返回 JSON 的 structured_output 字段,省掉自己解析和重试。
怎么做
claude -p "从 CHANGELOG 提取最新版本号" --output-format json \
--json-schema '{"type":"object","properties":{"version":{"type":"string"}},"required":["version"]}'
可信度
| 适用版本 | ≥ 2.1.84 |
|---|---|
| 验证于 | 2026年10月17日 · v2.1.296 · 本机实测 本机实际运行,structured_output 返回 {'prime': 7};changelog 最早在 2.1.84 提到该参数 |
| 依赖的命令/设置 | --json-schema-p |
| 来源 | https://code.claude.com/docs/en/cli-reference (official_docs) |