shell学习(12)- jq

jq命令允许直接在命令行下对JSON进行操作,包括分片、过滤、转换等 ,jq是用C编写,没有运行时依赖,所以几乎可以运行在任何系统上。预编译的二进制文件可以直接在Linux、OS X和windows系统上运行,当然在linux和OS X系统你需要赋与其可执行权限;在linux系统中也可以直接用yum安装。

下载地址 https://stedolan.github.io/jq/download/

本文主要介绍其使用,默认是已经安装好的,没有安装的,请自行安装。

现在先准备json串,如下kumufengchun.json:

{"name":"kumufengchun","age":"18","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": "18",
  "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"
"18"
"beijing"
"kumufengchun@gmail.com"
"Thursday"
"China"
[
  "baidu",
  "google",
  "alibaba"
]

 

6.输出多个索引的值,可以用逗号分割

语法: jq '.key1,.key2'

[root@localhost ~]$ jq '.name,.age' kumufengchun.json 
"kumufengchun"
"18"
[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 1 | jq '(.+2)*5' 
15
[root@localhost ~]$ echo {1,2,3} | jq '(.+2)*5' 
15
20
25

 

9.length求长度,如果是字符串是求的字符串的长度,如果是数组则求得是数组的长度

[root@localhost ~]$ cat kumufengchun.json | jq '.[] | length'
12
2
7
22
8
5
3

 

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'
[
  0,
  1,
  2
]

 

11.判断存不存在某个键

[root@localhost ~]$ cat kumufengchun.json | jq 'has("email")'
true
[root@localhost ~]$ cat kumufengchun.json | jq 'has("address")'
false

 

12.多个json

{

  "type""home",
  "number""212 555-1234"
}
{
  "type""fax",
  "number""646 555-4567"
}
jq -r '.phoneNumber[]|{number}' bo.json
jq -r '.phoneNumber[]|.number' bo.json

 

 

更多用法参见文档 https://stedolan.github.io/jq/manual/#Invokingjq

https://www.cnblogs.com/kevingrace/p/7565371.html

posted @ 2019-05-01 15:53  枯木逢春  阅读(5947)  评论(0编辑  收藏  举报