linux json 格式化工具 jq 详解
jq
是一个轻量级的命令行 json 处理工具,它非常灵活,对应简单的 json 处理不用写脚本使用 jq 就可以处理了,官网地址 https://stedolan.github.io/jq/
一. 安装
jq
在各大 linux 发行版、mac上的官方仓库都有,直接使用命令行安装即可,如果无法通过网络安装,直接在官网下载可执行文件放到系统环境变量即可使用。
sudo apt install -y jq
brew install jq
sudo wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O /usr/local/bin/jq
二. 格式化高亮展示
jq
最简单的使用是格式化高亮json,如下:
echo '{"name":"yzy", "age":18}' | jq .
{
"name": "yzy",
"age": 18
}
三. 提取指定字段值
- 提取指定字段
echo '{"name":"yzy", "age":18}' | jq .name
"yzy"
- 获取第一层所有 keys
echo '{"name":"yzy", "age":18}' | jq keys
[
"age",
"name"
]
- 获取第一层所有 keys 的 values
echo '{"name":"yzy", "info":{"age":18}}' | jq .[]
"yzy"
{
"age": 18
}
- 获取 values 组成新的数组
echo '{"name":"yzy", "age":18}' | jq [.[]]
[
"yzy",
18
]
- 取出数组的所有元素
echo '["1", "2", "3"]' | jq .[]
"1"
"2"
"3"
- 数组切片操作,切片操作类似 Python 等的数组切片操作
echo '["1", "2", "3"]' | jq .[0]
"1"
echo '["1", "2", "3"]' | jq .[1]
"2"
echo '["1", "2", "3"]' | jq .[2]
"3"
echo '["1", "2", "3"]' | jq .[3]
null
echo '["1", "2", "3"]' | jq .[:2]
[
"1",
"2"
]
echo '["1", "2", "3"]' | jq .[1:2]
[
"2"
]
- 数组元素继续操作
echo '[{"name": "foo"},{"name": "bar"}]' |jq .[].name
"foo"
"bar"
- 多个字段提取用逗号
,
分割
echo '{"name":"yzy", "age":18}' | jq '.name, .age'
"yzy"
18
- 多个字段提取用逗号
,
分割(在同一行展示,可以修改字段个数2,分隔符|)
echo '{"name":"yzy", "age":18}' | jq '.name, .age' | awk '{if(NR%2!=0)ORS="|";else ORS="\n"}1'
"yzy"|18
四. 高级用法
使用管道
- 数组元素提取(使用 jq 管道)
echo '[{"name": "foo"},{"name": "bar"}]' |jq '.[]|.name'
"foo"
"bar"
- 数组元素提取(使用 linux 管道)
echo '[{"name": "foo"},{"name": "bar"}]' |jq '.[]' | jq .name
"foo"
"bar"
使用函数
length
计算字符串长度
echo '{"name":"yzy", "address":"beijing"}' | jq '.name|length'
3
echo '{"name":"yzy", "address":"beijing"}' | jq '.[]|length'
3
7
map
函数对数组操作
echo '["1", "22", "333"]' | jq 'map(length)'
[
1,
2,
3
]
echo '["1", "22", "333"]' | jq 'map(length)|.[]'
1
2
3
select
函数进行过滤
echo '["1", "22", "333"]' | jq '.[]|select(length>1)'
"22"
"333"
join
函数进行多字段连接
echo '["zhang", "wang", "zhao"]' | jq '.|join(" | ")'
"zhang | wang | zhao"
split
函数将字符串切割成数组
echo '"zhang , wang,zhao"' | jq 'split(",")'
[
"zhang ",
" wang",
"zhao"
]
控制流程
- 使用
if .. then .. elif .. then .. else .. end
实现条件判断
# 使用map
echo '[0, 1, 2, 3]' | jq 'map(if . == 0 then "zero" elif . == 1 then "one" elif . == 2 then "two" else "many" end)'
[
"zero",
"one",
"two",
"many"
]
# 使用管道
echo '[0, 1, 2, 3]' | jq '.[]|if . == 0 then "zero" elif . == 1 then "one" elif . == 2 then "two" else "many" end'
"zero"
"one"
"two"
"many"
构造json
- 字典
echo '["zhang", "wang", "zhao"]' | jq '.[]|{name: ., address: "beijing"}'
{
"name": "zhang",
"address": "beijing"
}
{
"name": "wang",
"address": "beijing"
}
{
"name": "zhao",
"address": "beijing"
}
- 数组
echo '["zhang", "wang", "zhao"]' | jq '[.[]|{name: ., address: "beijing"}]'
[
{
"name": "zhang",
"address": "beijing"
},
{
"name": "wang",
"address": "beijing"
},
{
"name": "zhao",
"address": "beijing"
}
]
其他用法
- 字符串拼接(方法一:使用())
echo '{"name":"yzy", "address":"beijing"}' | jq '"hi \(.name)"'
"hi yzy"
- 字符串拼接(方法二:使用+)
echo '{"name":"yzy", "address":"beijing"}' | jq '"hi " + .name'
"hi yzy"
参考文档
https://mozillazg.com/2018/01/jq-use-examples-cookbook.html
https://stedolan.github.io/jq/manual/