![](/upload/rand_pic/2-968.jpg)
jq 是个非常强大的处理 JSON 数据的命令行工具。
安装Linux
1
yum install jq
macOS
1
brew install jq使用示例
注意:示例数据不是一段json数据,它的每一行都是一条json数据。每一行都会处理一次。
1
curl -s https://kekek.cc/static/jq.json | jq 'select (.["remote_addr"] == "187.141.142.230")'
select(boolean_expression) 为查询语句,.[ remote_addr ] 表示取对象的remote_addr属性(除了可以用.[]取值还可以直接.取值,前者可以处理特殊字符的情况)。 == 表示等于。
查询 method 为 GET 的请求,并且status 为 200
1
curl -s https://kekek.cc/static/jq.json | jq 'select (.["method"] == "GET" and .status == 200)'
12345
interface AccessLog ip: string; method: string; url: string;
1
curl -s https://kekek.cc/static/jq.json | jq '. | ip: .["remote_addr"], method: .method, url: .["request_uri"] '
这里使用了管道符 | 可以多次处理结果
输出csv格式1
curl -s https://kekek.cc/static/jq.json | jq '. | [.["remote_addr"], .method, .status]] | @csv'
.[] 表示取整个数组,一般配合管道符号一起使用 .[] | {ip: .[ remote_addr ], method: .method, url: .[ request_uri ]}
.[0] 表示取数组的第一个元素
参考