本文主要包括:
- json解析工具-jq简介
jq简介
jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地把玩文本。它能轻松地把你拥有的数据转换成你期望的格式,而且需要写的程序通常也比你期望的更加简短。
jq安装
在Linux下安装:
# 安装EPEL源:
yum install -y epel-release
# 安装完EPEL源后,可以查看下jq包是否存在:
yum list jq
# 安装jq:
yum install -y jq
使用案例
- 使用 jq 美化 json 数据
jsonStr='{"name":"springcloud","processDefinitionId":"","failureStrategy":"CONTINUE","warningType":"NONE","warningGroupId":"0","taskDependType":"TASK_POST","runMode":"RUN_MODE_SERIAL","processInstancePriority":"MEDIUM","workerGroup":"default","runParam":[{"T_SID":"tenantId","INS_NO":"inspectionNo","partitionFields":"partitionFields","batchNo":"111","groupBy":"battery_voltage,","orderBy":"d,e","startLine":"3","endLine":"63","calculationType":"1","matchNo":"aaaaaaaaa1","planNo":"bbbbbbbb2","sampleNo":"ccccccc3","analysisModelNo":"aaaa","startRowNo":"1","endRowNo":"1000"}]}' echo $jsonStr | jq . # 输出为: { "name": "springcloud", "processDefinitionId": "", "failureStrategy": "CONTINUE", "warningType": "NONE", "warningGroupId": "0", "taskDependType": "TASK_POST", "runMode": "RUN_MODE_SERIAL", "processInstancePriority": "MEDIUM", "workerGroup": "default", "runParam": [ { "T_SID": "tenantId", "INS_NO": "inspectionNo", "partitionFields": "partitionFields", "batchNo": "111", "groupBy": "battery_voltage,", "orderBy": "d,e", "startLine": "3", "endLine": "63", "calculationType": "1", "matchNo": "aaaaaaaaa1", "planNo": "bbbbbbbb2", "sampleNo": "ccccccc3", "analysisModelNo": "aaaa", "startRowNo": "1", "endRowNo": "1000" } ] }
- 获取key的值
echo $jsonStr | jq -r '.name' # 输出 springcloud # 输出多个索引的值,可以用逗号分割 echo $jsonStr | jq -r '.name,.failureStrategy'
- 数组操作
.[], .[i], .[s:e],[s:],[:e]
# 语法:jq '.[s:e]',返回的是数组或者列表的index从s开始(包括s)到e结束(不包括e) # jq '.[i]'代表取数组第i位,从0开始 # [[s:],[:e] 分别代表取从s开始直到结尾/从头开始直到e echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq -r '.[1]' # 输出值 { "name": "XML", "good": false }
- 构造数组/对象
[], {}
echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' | jq -r '{user, title: .titles[]}' # 输出: { "user": "stedolan", "title": "JQ Primer" } { "user": "stedolan", "title": "More JQ" }
- 括号的作用
# 可以直接计算加减乘除 echo 1 | jq '(.+2)*5' # 15 echo {1,2,3} | jq '(.+2)*5' # 15 20 25
- length求长度
#如果是字符串是求的字符串的长度,如果是数组则求得是数组的长度 echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq -r '. | length' # 2 echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false,"a":1}]' | jq -r '.[] | length' # 2 3 echo $jsonStr | jq -r '.name | length' # 11
- 输出所有的keys
echo $jsonStr | jq -r 'keys' # [ # "failureStrategy", # "name", # "processDefinitionId", # "processInstancePriority", # "runMode", # "runParam", # "taskDependType", # "warningGroupId", # "warningType", # "workerGroup" # ]
- 判断存不存在某个键
echo $jsonStr | jq 'has("name")' # true echo $jsonStr | jq 'has("aaa")' # false
- 字符插入
\(foo)
echo '42' | jq '"The input was \(.), which is one less than \(.+1)"' # "The input was 42, which is one less than 43"
- if-then-else-end 条件
echo '2' | jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end' # many