概述
json数据:
{
"CHANNEL.chn-live_hh":{
"a":1,
"b":2
},
"CHANNEL.chn-live_bb":{
"a":1,
"b":2
},
"SINK.chn-live_bb":{
"a":1,
"b":2
}
}
jq解析filter:
to_entries|reduce .[] as $item
({"channel":[],"sink":[]};
if $item.key|startswith("CHANNEL")
then
.+{"channel":(.channel+[$item.key|capture("(?<topic>[a-z]+_.+)")+{"a":$item.value.a,"b":$item.value.b}])}
else
.
end)
解析结果:
{
"channel": [
{
"topic": "live_hh",
"a": 1,
"b": 2
},
{
"topic": "live_bb",
"a": 1,
"b": 2
}
],
"sink": []
}
官方文档:
https://stedolan.github.io/jq/manual/
reduce .[] as $sink ({};if .|has($sink.topic) then .|setpath([$sink.topic,"BatchUnderflowCount"];$sink.BatchUnderflowCount+.[$sink.topic].BatchUnderflowCount)|setpath([$sink.topic,"BatchCompleteCount"];$sink.BatchCompleteCount+.[$sink.topic].BatchCompleteCount)|setpath([$sink.topic,"ConnectionFailedCount"];$sink.ConnectionFailedCount+.[$sink.topic].ConnectionFailedCount)|setpath([$sink.topic,"EventDrainAttemptCount"];$sink.EventDrainAttemptCount+.[$sink.topic].EventDrainAttemptCount)|setpath([$sink.topic,"ConnectionCreatedCount"];$sink.ConnectionCreatedCount+.[$sink.topic].ConnectionCreatedCount)|setpath([$sink.topic,"BatchEmptyCount"];$sink.BatchEmptyCount+.[$sink.topic].BatchEmptyCount)|setpath([$sink.topic,"ConnectionClosedCount"];$sink.ConnectionClosedCount+.[$sink.topic].ConnectionClosedCount)|setpath([$sink.topic,"EventDrainSuccessCount"];$sink.EventDrainSuccessCount+.[$sink.topic].EventDrainSuccessCount) else .|setpath([$sink.topic];{"BatchUnderflowCount":$sink.BatchUnderflowCount,"BatchCompleteCount":$sink.BatchCompleteCount,"ConnectionFailedCount":$sink.ConnectionFailedCount,"EventDrainAttemptCount":$sink.EventDrainAttemptCount,"ConnectionCreatedCount":$sink.ConnectionCreatedCount,"BatchEmptyCount":$sink.BatchEmptyCount,"ConnectionClosedCount":$sink.ConnectionClosedCount,"EventDrainSuccessCount":$sink.EventDrainSuccessCount}) end)
最后
以上就是优雅芒果为你收集整理的shell jq 解析json的全部内容,希望文章能够帮你解决shell jq 解析json所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复