我是靠谱客的博主 优雅芒果,最近开发中收集的这篇文章主要介绍shell jq 解析json,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部