linux jq命令详解
jq 是一个强大的命令行工具,用于处理 JSON 格式的数据。它可以帮助你查询、过滤、修改和处理 JSON 数据,使得在命令行环境下处理 JSON 变得非常方便。
安装
yum install -y jq
命令
jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
options
Some of the options include:--compact-output, -c # 以压缩格式输出--null-input, -n use `null` as the single input value;-e # 基于输出设置 exit 状态(set the exit status code based on the output);--slurp , -s read (slurp) all inputs into an array; apply filter to it;--raw-output, -r #输出原始字符串,不是json格式。(output raw strings, not JSON texts);--join-output , -j--raw-input, -R #读原始字符串,而不是json文本( read raw strings, not JSON texts);-C #使用彩色。colorize JSON;-M #不使用彩色。 monochrome (don't colorize JSON);--sort-keys , -S #输出key排序。(sort keys of objects on output);--indent n # 指定n个空格作为缩进--tab # use tabs for indentation;--arg a v # set variable $a to value <v>;-f filename / --from-file filename #从文件读取filter--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 # 把参数解析为json文本,而不是文件名。 remaining arguments are JSON arguments, not files;-- #参数结束 terminates argument processing;
filter
.
: 表示当前对象,用于访问字段或属性。,
:多个filter|
:管道.fieldName
: 选择指定字段的值。[fieldName]
: 选择指定字段的值。[fieldName]?
: 选择指定字段的值,属性不存在则不报错,返回null.fieldName?
: 选择指定字段的值,属性不存在则不报错,返回null[]
: 用于遍历数组元素,返回iterator。[<number>]
: 用于选择元素。.[<number>:<number>]
:切片select(condition)
: 根据条件选择元素。map(transform)
: 对数组中的每个元素应用转换操作。
示例
测试数据
{"code": 0,"msg": null,"data": [{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X", "port": 8888},{"ip": "192.168.1.X", "port": 8889}]},{"name": "模型服务","entry": "/jmodel/","instances": [{"ip": "192.168.1.X", "port": 8899},{"ip": "192.168.1.X", "port": 8898}]}]
}
输出参数
▶ json格式化:
cat data.json | jq ' .data[0]'#json格式化
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}
▶ 压缩输出:
cat data.json | jq -c ' .data[0]'
{"name":"采集服务","entry":"/jcollect/","instances":[{"ip":"192.168.1.X","port":8888},{"ip":"192.168.1.70","port":8889}]}
▶ -r:
cat data.json | jq -r ' .data[0]'
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}
▶ tab缩进:
cat data.json | jq -r --tab ' .data[0]'
{"name": "采集服务","entry": "/jcollect/","instances": [{"ip": "192.168.1.X","port": 8888},{"ip": "192.168.1.X","port": 8889}]
}
基础Filter
.
,[]
,.fieldName
cat data.json | jq ' .data[].name'
"采集服务"
"模型服务"
[number]
cat data.json | jq ' .data[].name'
"采集服务"
"模型服务"
构造器
- 数组构造器:
[]
- 对象构造器:
{}
..
:路径打平,类似XPath
的//
内置operator和function
+
:-
:*
:/
:%
:- abs
- length
- utf8bytelength
- keys, keys_unsorted
- has(key)
- in
- map(f), map_values(f)
- pick(pathexps)
- path(path_expression)
- del(path_expression)
- getpath(PATHS)
- setpath(PATHS; VALUE)
- delpaths(PATHS)
- …
条件和比较
==
,!=
if-then-else-end
>, >=, <=, <
and, or, not
//
:Alternative operator
正则表达式
test(val)
test(regex; flags)
match(val)
match(regex; flags)
- …
附录
参考
GitHub 地址:https://github.com/stedolan/jq
jq 官方网站: https://stedolan.github.io/jq/
文档:https://jqlang.github.io/jq/tutorial/
手册:https://jqlang.github.io/jq/manual/#invoking-jq
在线工具:https://jqplay.org/