jq命令允许直接在命令行下对JSON进行操作,包括分片、过滤、转换等 ,jq是用C编写,没有运行时依赖,所以几乎可以运行在任何系统上。预编译的二进制文件可以直接在Linux、OS X和windows系统上运行,当然在linux和OS X系统你需要赋与其可执行权限;在linux系统中也可以直接用yum安装。
下载地址 https://stedolan.github.io/jq/download/
本文主要介绍其使用,默认是已经安装好的,没有安装的,请自行安装。
常用选项
-c 紧凑输出json数据
-e 根据输出结果设置命令退出状态码
-s 读取所有输入到一个数组
-r 输出原始字符串,而不是一个JSON格式
-C 高亮显示
-M 单色显示
-S 排序对象
--tab 使用tab缩进
现在先准备json串,如下kumufengchun.json:
{"name":"kumufengchun","age":"","city":"beijing","email":"kumufengchun@gmail.com","date":"Thursday","country":"China","company":["baidu","google","alibaba"]}"
如下test.json:
[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
1.用jq .直接查看
jq . kumufengchun.json
或者
cat kumufengchun.json | jq .
输出如下:
[root@localhost ~]$ cat kumufengchun.json | jq .
{
"name": "kumufengchun",
"age": "",
"city": "beijing",
"email": "kumufengchun@gmail.com",
"date": "Thursday",
"country": "China",
"company": [
"baidu",
"google",
"alibaba"
]
}
是已经格式化的json数据串,在linux系统下还高光显示,key和value用不同的颜色表示,如下图:
2.输出某个字段或者某个索引的值
语法:jq '.<key>' ,这里key是字段名称
[root@localhost ~]$ jq .name kumufengchun.json
"kumufengchun"
[root@localhost ~]$ jq '.name' kumufengchun.json
"kumufengchun"
[root@localhost ~]$ jq '.company' kumufengchun.json
[
"baidu",
"google",
"alibaba"
]
3.输出数组的值
语法:jq '.[<value>]' ,这里value是数组的索引整数值
[root@localhost ~]$ jq '.company[1]' kumufengchun.json
"google"
[root@localhost ~]$ jq '.company[2]' kumufengchun.json
"alibaba"
4.输出列表、数组的一部分,对其进行切片
语法:jq '.<list-key>[s:e]',返回的是数组或者列表的index从s开始(包括s)到e结束(不包括e)
[root@localhost ~]$ jq '.company[0:2]' kumufengchun.json
[
"baidu",
"google"
]
也可以省略开始的index,只有结束的index,如下,仍然是不包括结束index的值:
[root@localhost ~]$ jq '.company[:3]' kumufengchun.json
[
"baidu",
"google",
"alibaba"
]
也可以省略结束的index,只有开始的index,如下,输出到最后:
[root@localhost ~]$ jq '.company[1:]' kumufengchun.json
[
"google",
"alibaba"
]
开始的索引也可以是负数,表示从后边倒着数,从-1开始数:
[root@localhost ~]$ jq '.company[-2:]' kumufengchun.json
[
"google",
"alibaba"
]
5.循环输出所有的值,如数组嵌套
语法:jq '.[]'
[root@localhost ~]$ jq '.[]' test.json
{
"name": "JSON",
"good": true
}
{
"name": "XML",
"good": false
}
[root@localhost ~]$ jq '.[]' kumufengchun.json
"kumufengchun"
""
"beijing"
"kumufengchun@gmail.com"
"Thursday"
"China"
[
"baidu",
"google",
"alibaba"
]
6.输出多个索引的值,可以用逗号分割
语法: jq '.key1,.key2'
[root@localhost ~]$ jq '.name,.age' kumufengchun.json
"kumufengchun"
""
[root@localhost ~]$ jq '.date,.company[]' kumufengchun.json
"Thursday"
"baidu"
"google"
"alibaba"
如果是数组,用中括号括起来要输出的键值,键值先写谁,先输出谁
[root@localhost ~]$ jq '.company[2,0]' kumufengchun.json
"alibaba"
"baidu"
7.用管道符号|可以对其进行再次处理
语法:jq .[] | .<key1>
[root@localhost ~]$ jq '.[]|.name' test.json
"JSON"
"XML"
8.括号的作用
[root@localhost ~]$ echo | jq '(.+2)*5'
[root@localhost ~]$ echo {,,} | jq '(.+2)*5'
9.length求长度,如果是字符串是求的字符串的长度,如果是数组则求得是数组的长度
[root@localhost ~]$ cat kumufengchun.json | jq '.[] | length'
10.输出所有的keys
语法: jq keys
[root@localhost ~]$ cat kumufengchun.json | jq 'keys'
[
"age",
"city",
"company",
"country",
"date",
"email",
"name"
]
输出数组的keys:
[root@localhost ~]$ cat kumufengchun.json | jq '.company | keys'
[
,
,
]
11.判断存不存在某个键
[root@localhost ~]$ cat kumufengchun.json | jq 'has("email")'
true
[root@localhost ~]$ cat kumufengchun.json | jq 'has("address")'
false
12.向josn文件中追加数据
实例1:以 /etc/docker/daemon.json 为例
原本数据如下:
{
"dns": [
"114.114.114.114",
"8.8.8.8"
],
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
执行追加命令
cat /etc/docker/daemon.json | jq '.["log-driver"]="json-file"' > tmp.json && mv -f tmp.json /etc/docker/daemon.json
cat /etc/docker/daemon.json | jq '.["log-opts"]={"max-size":"500m", "max-file":"3"}' > tmp.json && mv -f tmp.json /etc/docker/daemon.json
最终数据如下:
{
"dns": [
"114.114.114.114",
"8.8.8.8"
],
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "500m",
"max-file": "3"
}
}
实例2:
objName="objName"
objJSON='{"test": "json"}'
echo '{"data":{}}' |
jq --arg objectName "$objName" \
--argjson jsonString "$objJSON" \
'.data[$objectName] += $jsonString'
输出:
{
"data": {
"objName": {
"test": "json"
}
}
}