jq 命令详解
命令jq
可以对 json
数据 进行分片、过滤、映射和转换
它能轻松地把你拥有的数据转换成你期望的格式,而且需要写的程序通常也比你期望的更加简短
=
总结
- 需要用引号括起所有内容(单双引号均可)
- 若无管道操作,可省略引号
- 若有管道操作,必须使用引号和括号(即使不指定元素也要括号
.[]
)
= =
= =
安装
yum -y install jq
=
命令格式
# 格式
cat json.txt | jq 'cmd'
# 不同的 cmd 可对数据进行不同的过滤
# 基础用法:以下三者等效
cat json.txt | jq '.[]' # 标准格式。。。
cat json.txt | jq '.'
cat json.txt | jq .
演示:
# 源文件(可读性差)
[root@centos7 ~]#cat json.txt
[{"name":"站长工具","url":"http://tool.chinaz.com","address":{"city":"厦门","country":"中国"},"arrayBrowser":[{"name":"Google","url":"http://www.google.com"},{"name":"Baidu","url":"http://www.baidu.com"}]},{"name":"站长之家","url":"http://tool.zzhome.com","address":{"city":"大连","country":"中国"},"arrayBrowser":[{"name":"360","url":"http://www.so.com"},{"name":"bing","url":"http://www.bing.com"}]}]
# 命令 jq . 实现优美输出
[root@centos7 ~]#cat json.txt | jq .
[
{
"name": "站长工具",
"url": "http://tool.chinaz.com",
"address": {
"city": "厦门",
"country": "中国"
},
"arrayBrowser": [
{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
}
]
},
{
"name": "站长之家",
"url": "http://tool.zzhome.com",
"address": {
"city": "大连",
"country": "中国"
},
"arrayBrowser": [
{
"name": "360",
"url": "http://www.so.com"
},
{
"name": "bing",
"url": "http://www.bing.com"
}
]
}
]
=
=
指定输出列表的第几个元素
注:.[0]
表示第一个。。。(引号可省略)
[root@centos7 ~]#cat json.txt | jq .[0]
{
"name": "站长工具",
"url": "http://tool.chinaz.com",
"address": {
"city": "厦门",
"country": "中国"
},
"arrayBrowser": [
{
"name": "Google",
"url": "http://www.google.com"
},
{
"name": "Baidu",
"url": "http://www.baidu.com"
}
]
}
# 只输出第二个
[root@centos7 ~]#cat json.txt | jq .[1]
{
"name": "站长之家",
"url": "http://tool.zzhome.com",
"address": {
"city": "大连",
"country": "中国"
},
"arrayBrowser": [
{
"name": "360",
"url": "http://www.so.com"
},
{
"name": "bing",
"url": "http://www.bing.com"
}
]
}
# 不存在则输出 null
[root@centos7 ~]#cat json.txt | jq .[2]
null
=
过滤指定的字段
- 有管道操作时,必须使用引号、必须使用元素括号
- 引号用来将
jq
之后的所有内容括起来
标准格式:
# 不限定元素
cat json.txt | jq '.[] | {name-1:.aabb,name-2:.xx.yy}'
# 解释。。。。。。。。。。。。
name-1 、 name-2 为'自定义key'
.aabb 、 .xx.yy 为源文本中存在的字段的值
#------------------------------------------------------#
# 指定第几个元素再过滤
cat json.txt | jq '.[0] | {name:.name,city:.address.city}'
cat json.txt | jq '.[1] | {name:.arrayBrowser[1].name,city:.address.city}'
效果:
# 全文操作
[root@centos7 ~]#cat json.txt | jq '.[] | {n1:.name,n2:.address.city,n3:.address.country}'
{
"n1": "站长工具",
"n2": "厦门",
"n3": "中国"
}
{
"n1": "站长之家",
"n2": "大连",
"n3": "中国"
}
# 指定元素过滤
[root@centos7 ~]#cat json.txt | jq '.[0] | {n1:.name,n2:.address.city,n3:.address.country}'
{
"n1": "站长工具",
"n2": "厦门",
"n3": "中国"
}
[root@centos7 ~]#cat json.txt | jq '.[1] | {n1:.name,n2:.address.city,n3:.address.country}'
{
"n1": "站长之家",
"n2": "大连",
"n3": "中国"
}
=
输出数组
操作:在引号内部使用[]
将所有内容括起来
# 格式
cat json.txt | jq "[ cmd ]"
# 范例
cat json.txt | jq "[.[] | {name:.arrayBrowser[1].name,city:.address.city}]"
数组输出对比效果:
# 不用数组:
[root@centos7 ~]#cat json.txt | jq ".[] | {name:.arrayBrowser[1].name,city:.address.city}"
{
"name": "Baidu",
"city": "厦门"
}
{
"name": "bing",
"city": "大连"
}
# 使用数组:
[root@centos7 ~]#cat json.txt | jq "[.[] | {name:.arrayBrowser[1].name,city:.address.city}]"
[
{
"name": "Baidu",
"city": "厦门"
},
{
"name": "bing",
"city": "大连"
}
]
=
自定义 key
在{}
中,冒号前面的名字是映射的名称,可以任意修改
cat json.txt | jq "[.[] | {name_001:.arrayBrowser[1].name,city_002:.address.city}]"
cat json.txt | jq ".[] | {CAOKUNZI_001:.arrayBrowser[1].name,ZHUBAZI_002:.address.city}"
演示:
[root@centos7 ~]#cat json.txt | jq "[.[] | {name_001:.arrayBrowser[1].name,city_002:.address.city}]"
[
{
"name_001": "Baidu",
"city_002": "厦门"
},
{
"name_001": "bing",
"city_002": "大连"
}
]
[root@centos7 ~]#cat json.txt | jq ".[] | {CAOKUNZI_001:.arrayBrowser[1].name,ZHUBAZI_002:.address.city}"
{
"CAOKUNZI_001": "Baidu",
"ZHUBAZI_002": "厦门"
}
{
"CAOKUNZI_001": "bing",
"ZHUBAZI_002": "大连"
}
wan