jq
它能做什么
这项技能帮助你从 JSON 数据中提取、筛选和重组信息。适合处理 API 响应、配置文件或日志、并且只需要其中某项特定数据的人。
适用场景
- ✓当你需要从 JSON 响应中提取一个字段或一份筛选后的列表的时候。
- ✓当你想把 JSON 数据重组为另一种结构的时候。
- ✓当你在阅读 JSON 格式的配置文件或日志,需要快速得到答案的时候。
技能链接
https://github.com/charmbracelet/crush/blob/main/internal/skills/builtin/jq/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请从这个 API 响应中,只给我所有活跃用户的姓名和电子邮件地址,并以列表形式呈现。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会编写一个 jq 过滤器,对 JSON 进行选择、筛选或重建,并返回结果。内置的 jq 支持原始输出和紧凑输出等常用选项。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
jq — Built-in JSON Processor
Crush ships a built-in jq command (via github.com/itchyny/gojq) available
in the bash tool. No external binary is required.
Supported Flags
| Flag | Description |
|---|---|
-r, --raw-output |
Output strings without quotes |
-j, --join-output |
Like -r but no trailing newline |
-c, --compact-output |
One-line JSON output |
-s, --slurp |
Read all inputs into an array |
-n, --null-input |
Use null as input (ignore stdin) |
-e, --exit-status |
Exit 1 if last output is false or null |
-R, --raw-input |
Read each line as a string, not JSON |
--arg name value |
Bind $name to a string value |
--argjson name value |
Bind $name to a parsed JSON value |
File arguments after the filter are also supported: jq '.foo' file.json.
Differences from Standard jq
The built-in uses gojq, which is a pure-Go jq implementation. Key differences:
- No object key ordering — keys are sorted by default;
keys_unsortedand-Sare unavailable. - Arbitrary-precision integers — large integers keep full precision (addition, subtraction, multiplication, modulo, division when divisible).
- String indexing —
"abcde"[2]returns"c". - Not supported —
--ascii-output,--seq,--stream,--stream-errors,-f/--from-file,--slurpfile,--rawfile,--args,--jsonargs,input_line_number,$__loc__, some regex features (backreferences, look-around). - YAML — gojq supports
--yaml-input/--yaml-outputbut the built-in does not currently expose these flags.
Common Patterns
Extract a field:
echo '{"name":"crush"}' | jq '.name'
Filter an array:
echo '[1,2,3,4,5]' | jq '[.[] | select(. > 3)]'
Reshape objects:
echo '{"first":"Ada","last":"Lovelace"}' | jq '{full: (.first + " " + .last)}'
Use variables:
echo '{}' | jq --arg host localhost --argjson port 8080 '{host: $host, port: $port}'
Slurp multiple JSON values:
echo '{"a":1}{"b":2}' | jq -s '.'
Compact output for piping:
echo '{"a":1}' | jq -c '.a += 1'
Raw string output:
echo '["one","two","three"]' | jq -r '.[]'
Process a file:
jq '.dependencies | keys' package.json
Null input for constructing JSON:
jq -n --arg msg hello '{"message": $msg}'
Tips
- Pipe jq output to other commands:
jq -r '.url' data.json | xargs curl - Chain filters with
|inside the expression, not shell pipes. - Use
tryto suppress errors on missing keys:jq 'try .foo.bar' - Use
// "default"for fallback values:jq '.name // "unknown"' - Use
@csv,@tsv,@base64,@html,@urifor format strings.
常见问题
我需要单独安装 jq 吗?
不需要,jq 命令是内置的,无需外部程序。
它的行为和标准 jq 完全一样吗?
基本一样,但对象键默认会被排序,而且某些标准选项不可用。
它既能读取文件,也能读取管道数据吗?
可以,你可以在过滤器之后传入一个文件名。
详情
作者的原始描述
Use when the user needs to query, filter, reshape, extract, create, or construct JSON data — including API responses, config files, log output, or any structured data — or when helping the user write or debug JSON transformations.
repo: charmbracelet/crush
热度
- GitHub 星标
- 29k
- 排名
- #9767
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …