一、jq简介
jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地把玩文本。它能轻松地把你拥有的数据转换成你期望的格式,而且需要写的程序通常也比你期望的更加简短。 jq是用C编写,没有运行时依赖,所以几乎可以运行在任何系统上。预编译的二进制文件可以直接在Linux、OS X和windows系统上运行,当然在linux和OS X系统你需要赋与其可执行权限;在linux系统中也可以直接用yum安装。
在知道jq命令之前,我在linux系统中极少直接去命令去处理json数据,除非只是简单地从中过滤某个字符串,那就用grep结合正则表达式来解决。所以,掌握了jq命令,则可以让linux命令和shell脚本在处理json数据时变得得心应手。
二、jq 命令安装
yum install -y jq
官网下载二进制文件:
下载页面: https://stedolan.github.io/jq/download/
wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 chmod +x ./jq cp jq /usr/bin
三、参数说明
选项 | 解释 | 备注 |
---|---|---|
-c | compact instead of pretty-printed output | |
-n | use null as the single input value | |
-e | set the exit status code based on the output | |
-s | read (slurp) all inputs into an array; apply filter to it | 使用-s 选项,jq 会将所有的 JSON 输入放入一个数组中并在这个数组上使用 filter。"-s"选项不但影响到 filter 的写法。如果在 filter 中需要对数据进行选择和映射,其还会影响最终结果。 |
-r | output raw strings, not JSON texts | 假设我们要查询 JSON 字符串{"name":"tom"}中 name 的值. 使用-r 选项时返回的是tom. 不使用-r 选项时,返回的是"tom".返回值多了一对双引号 |
-R | read raw strings, not JSON texts | |
-M | monochrome (don't colorize JSON) | |
-S | sort keys of objects on output | |
--tab | use tabs for indentation | 使用Tab缩进 |
--arg a v | set variable $a to value <v> | 通过该选项提供了和宿主脚本语言交互的能力。该选项将值(v)绑定到一个变量(a)上。在后面的 filter 中可以直接通过变量引用这个值。例如,filter '.$a'表示查询属性名称等于变量 a 的值的属性。 |
--argjson a v | set variable $a to JSON value <v> | |
--slurpfile a f | set variable $a to an array of JSON texts read from <f> | |
--rawfile a f | set variable $a to a string consisting of the contents of <f> | |
--args | remaining arguments are string arguments, not files | |
--jsonargs | remaining arguments are JSON arguments, not files |
四、jq简明教程
例子文件 为了便于演示jq的功能,我们在文件json.txt中保存如下内容:
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的效果,在json解析工具中显示为:
演示
最简单的jq程序是表达式".",它不改变输入,但可以将其优美地输出,便于阅读和理解。
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"
}
]
}
]
[index],'.[]' 返回所有的元素
输出列表中的第一个元素,可以使用[index]:
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"
}
]
}
jq 管道线 |
jq支持管道线 |,它如同linux命令中的管道线——把前面命令的输出当作是后面命令的输入。如下命令把.[0]作为{…}的输入,进而访问嵌套的属性,如.name和.address.city。(有点类似 css 里获取属性的方法)
实例:
观察如下几个命令,通过改变|前后的输入和输出来达到不同的效果: 第一个 name 为显示的名称,可以随意写,:后边的是要查询的属性,.属性名
cat json.txt | jq '.[0] | {name:.name,city:.address.city}'
{
"name": "站长工具",
"city": "厦门"
}
cat json.txt | jq '.[0] | {name:.arrayBrowser[1].name,city:.address.city}'
{
"name": "Baidu",
"city": "厦门"
}
cat json.txt | jq ".[] | {name:.arrayBrowser[1].name,city:.address.city}"
{
"name": "Baidu",
"city": "厦门"
}
{
"name": "bing",
"city": "大连"
}
jq []
如果希望把jq的输出当作一个数组,可以在前后加上[]:
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}]"
[
{
"name_001": "Baidu",
"city_002": "厦门"
},
{
"name_001": "bing",
"city_002": "大连"
}
]
扩展阅读
http://www.json.cn/wiki.html https://stedolan.github.io/jq/tutorial/
原文链接:
https://blog.csdn.net/qq_26502245/article/details/100191694