📖 jq JSON 查询速查

按使用场景分组,点击"复制"按钮一键复制命令

基础取值
美化输出(缩进格式化)
jq . data.json
取嵌套字段
jq '.user.address.city' data.json
取数组第 N 个元素
jq '.[1].name' items.json
取数组长度
jq 'length' items.json
列出对象所有键
jq '.user | keys' data.json
迭代与过滤
遍历数组元素
jq '.[] | .name' items.json
按数值条件筛选
jq '.[] | select(.price > 200)' items.json
按字段值筛选
jq '.[] | select(.category == "service")' items.json
多条件组合筛选
jq '.[] | select(.category == "hardware" and .stock > 0)' items.json
变换与构造
映射改写每个元素
jq 'map(.price |= . + 10)' items.json
构造新对象
jq '.[0] | {id, name, price}' items.json
字符串拼接
jq -r '.[0] | .name + " / " + .category' items.json
数字转字符串 tostring
jq '.user.age | tostring' data.json
字符串转数字 tonumber
jq '.user.ageText | tonumber' data.json
聚合与排序
数组求和与最大值
jq '[.[].price] | {sum: add, max: max}' items.json
数组去重
jq '[.[].tag] | unique' items.json
按字段排序
jq 'sort_by(.price) | .[] | {name, price}' items.json
按字段分组计数
jq 'group_by(.category) | map({category: .[0].category, count: length})' items.json
实用技巧
转 Base64
jq -r '.user.name | @base64' data.json
raw 输出(去掉引号)
jq -r '.user.name' data.json
null 值用默认值兜底
jq '.user.email // "not set"' data.json
取不存在的键不报错
jq '.user.phone? // "no such key"' data.json
读取多行 JSON(-s 合并)
jq -s 'map(.id)' records.jsonl

📖 jq JSON 查询速查 使用说明

本页收录 jq JSON 查询速查 的常用命令、参数与典型用法,按场景分组,每条命令可一键复制到终端。如果命令行里反复踩坑、参数记混,不妨先去在线工具里实时校验一下。

更多命令行场景可参考本站的 命令速查总目录,或尝试本站的 在线工具集——正则测试、时间戳转换、Cron 解析、HTTP 状态码查询等高频问题都能在浏览器里直接解决。

相关在线工具