shell学习(12)- jq
Posted kumufengchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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":"[email protected]","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 .
输出如下:
[[email protected] ~]$ cat kumufengchun.json | jq . { "name": "kumufengchun", "age": "18", "city": "beijing", "email": "[email protected]", "date": "Thursday", "country": "China", "company": [ "baidu", "google", "alibaba" ] }
是已经格式化的json数据串,在linux系统下还高光显示,key和value用不同的颜色表示,如下图:
2.输出某个字段或者某个索引的值
语法:jq ‘.<key>‘ ,这里key是字段名称
[[email protected] ~]$ jq .name kumufengchun.json "kumufengchun" [[email protected] ~]$ jq ‘.name‘ kumufengchun.json "kumufengchun" [[email protected] ~]$ jq ‘.company‘ kumufengchun.json [ "baidu", "google", "alibaba" ]
3.输出数组的值
语法:jq ‘.[<value>]‘ ,这里value是数组的索引整数值
[[email protected] ~]$ jq ‘.company[1]‘ kumufengchun.json "google" [[email protected] ~]$ jq ‘.company[2]‘ kumufengchun.json "alibaba"
4.输出列表、数组的一部分,对其进行切片
语法:jq ‘.<list-key>[s:e]‘,返回的是数组或者列表的index从s开始(包括s)到e结束(不包括e)
[[email protected] ~]$ jq ‘.company[0:2]‘ kumufengchun.json [ "baidu", "google" ]
也可以省略开始的index,只有结束的index,如下,仍然是不包括结束index的值:
[[email protected] ~]$ jq ‘.company[:3]‘ kumufengchun.json [ "baidu", "google", "alibaba" ]
也可以省略结束的index,只有开始的index,如下,输出到最后:
[[email protected] ~]$ jq ‘.company[1:]‘ kumufengchun.json [ "google", "alibaba" ]
开始的索引也可以是负数,表示从后边倒着数,从-1开始数:
[[email protected] ~]$ jq ‘.company[-2:]‘ kumufengchun.json [ "google", "alibaba" ]
5.循环输出所有的值,如数组嵌套
语法:jq ‘.[]‘
[[email protected] ~]$ jq ‘.[]‘ test.json { "name": "JSON", "good": true } { "name": "XML", "good": false } [[email protected] ~]$ jq ‘.[]‘ kumufengchun.json "kumufengchun" "18" "beijing" "[email protected]" "Thursday" "China" [ "baidu", "google", "alibaba" ]
6.输出多个索引的值,可以用逗号分割
语法: jq ‘.key1,.key2‘
[[email protected] ~]$ jq ‘.name,.age‘ kumufengchun.json "kumufengchun" "18"
[[email protected] ~]$ jq ‘.date,.company[]‘ kumufengchun.json "Thursday" "baidu" "google" "alibaba"
如果是数组,用中括号括起来要输出的键值,键值先写谁,先输出谁
[[email protected] ~]$ jq ‘.company[2,0]‘ kumufengchun.json "alibaba" "baidu"
7.用管道符号|可以对其进行再次处理
语法:jq .[] | .<key1>
[[email protected] ~]$ jq ‘.[]|.name‘ test.json "JSON" "XML"
8.括号的作用
[[email protected] ~]$ echo 1 | jq ‘(.+2)*5‘ 15
[[email protected] ~]$ echo {1,2,3} | jq ‘(.+2)*5‘ 15 20 25
9.length求长度,如果是字符串是求的字符串的长度,如果是数组则求得是数组的长度
[[email protected] ~]$ cat kumufengchun.json | jq ‘.[] | length‘ 12 2 7 22 8 5 3
10.输出所有的keys
语法: jq keys
[[email protected] ~]$ cat kumufengchun.json | jq ‘keys‘ [ "age", "city", "company", "country", "date", "email", "name" ]
输出数组的keys:
[[email protected] ~]$ cat kumufengchun.json | jq ‘.company | keys‘ [ 0, 1, 2 ]
11.判断存不存在某个键
[[email protected] ~]$ cat kumufengchun.json | jq ‘has("email")‘ true [[email protected] ~]$ cat kumufengchun.json | jq ‘has("address")‘ false
更多用法参见文档 https://stedolan.github.io/jq/manual/#Invokingjq
以上是关于shell学习(12)- jq的主要内容,如果未能解决你的问题,请参考以下文章