jq用法
Linux下处理json字符串利器,用过的人都说好!
1 测试用json文件
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json
{"status":"UP","components":{"db":{"status":"UP","details":{"database":"PostgreSQL","validationQuery":"isValid()"}},"discoveryComposite":{"status":"UP","components":{"discoveryClient":{"status":"UP","details":{"services":["gc-system-service","gc-policy-service","gc-gateway","gc-apply-service","gc-attachment-service","gc-party-service","gc-thirdparty-service","gc-travel-service","gc-message-service","gc-app-service","gc-endorse-service","gc-platform-service","gc-renewal-service","gc-product-service","gc-client-service","gc-workflow-service","gc-certify-service","gc-report-service","gc-auth-service","gc-payment-service","gc-agent-service","gc-claim-service","gc-finance-service","gc-batch-service"]}}}},"diskSpace":{"status":"UP","details":{"total":48420556800,"free":26521001984,"threshold":10485760,"exists":true}},"nacosConfig":{"status":"UP"},"nacosDiscovery":{"status":"UP"},"ping":{"status":"UP"},"refreshScope":{"status":"UP"},"sentinel":{"status":"UNKNOWN","details":{"dataSource":{"degrade-sentinel-nacos-datasource":{"status":"UP"},"authority-sentinel-nacos-datasource":{"status":"UP"},"flow-sentinel-nacos-datasource":{"status":"UP"},"param-flow-sentinel-nacos-datasource":{"status":"UP"},"system-sentinel-nacos-datasource":{"status":"UP"}},"enabled":true,"dashboard":{"description":"the dashboard servers [[Tuple2{r1=192.168.11.74, r2=13060}]] one of them can't be connected","status":"UNKNOWN"}}}}}
注意:
json 数组的键命名必须为下划线"_",不能为"-",否则解析不了
2 用法
2.1 根据key获取value:
- 说明
# 格式化json
cat service_health_return.json | jq .
# 第一级key值的获取(替换'{某key}'为具体key)
cat service_health_return.json | jq .{某key}
# 第N级key值的获取(替换'{某key*}'为具体key)
cat service_health_return.json | jq .{某key1}.{某key2}.{某keyN}
# 输出数组中的值
cat service_health_return.json | jq .{某key1}.{某key2}.{某keyN}[]
注意:
在使用中发现:jq后面的参数加不加【‘’】都行
解析不存在的元素,会返回null
- 示例:
# 格式化输入(service_health_return.json太长)
^_^ kevin@TM1701-b38cbc23:~$ echo '{"foo": 0}' | jq .
{
"foo": 0
}
# 获取key的值
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json |jq .db
"UP"
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json |jq .components.db
{
"status": "UP",
"details": {
"database": "PostgreSQL",
"validationQuery": "isValid()"
}
}
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json |jq .components.discoveryComposite.components.discoveryClient.details.services
[
"gc-system-service",
"gc-gateway",
"gc-finance-service",
"gc-batch-service"
]
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json |jq .components.discoveryComposite.components.discoveryClient.details.services[]
"gc-system-service"
"gc-gateway"
"gc-finance-service"
"gc-batch-service"
2.2 内建函数:keys,has
- 说明
- keys 是用来获取JSON中的key元素的,查找json中所有的键
- has 是用来是判断是否存在某个key
- 示例
# 获取key
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json | jq 'keys'
[
"components",
"status"
]
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json | jq 'keys' | jq .[]
"components"
"status"
# 获取下一级key
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json | jq .components | jq 'keys'
[
"db",
"discoveryComposite",
"diskSpace",
"nacosConfig",
"nacosDiscovery",
"ping",
"refreshScope",
"sentinel"
]
# 查询是否存在key
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json | jq 'has("components")'
true
^_^ kevin@TM1701-b38cbc23:~$ cat service_health_return.json | jq 'has("db")'
false
3 Help输出
^_^ kevin@TM1701-b38cbc23:~$ jq --help
jq - commandline JSON processor [version 1.6]
Usage: jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
其中一些选项包括:
-c 紧凑,而不是漂亮的打印输出;
-n 使用“null”作为单个输入值;
-e 根据输出设置退出状态代码;
-s 将所有输入读取(slurp)到一个数组中;对其应用过滤器;
-r 输出原始字符串,而不是JSON文本;
-R 读取原始字符串,而不是JSON文本;
-C 为JSON着色;
-M 单色(不要将JSON着色);
-S 对输出对象的键进行排序;
--tab 使用制表符进行缩进;
--arg a v 将变量$a设置为值<v>;
--argjson a v 将变量$a设置为JSON值<v>;
--slurpfile a f 将变量$a设置为从<f>读取的JSON文本数组;
--rawfile a f 将变量$a设置为由<f>的内容组成的字符串;
--args 剩余的参数是字符串参数,而不是文件;
--jsonargs 剩余的参数是JSON参数,而不是文件;
-- 终止自变量处理;
命名参数也可用作$ARGS.Named[],而位置参数可用作$ARGS.positional[]。
4 最后
爱你