【问题标题】:Using jq group_by and count to sort JSON output from curl into sorted and counted list使用 jq group_by 和 count 将 JSON 输出从 curl 排序到排序和计数列表
【发布时间】:2021-03-11 09:21:56
【问题描述】:

谁能帮助我理解和协助 jq 脚本(只使用 jq)?谁能帮我写一个脚本来获取以下输出:

`curl --silent "https://api.surfshark.com/v3/server/clusters" | jq` 

并列出每个 countryCode 中的服务器数量以及这些服务器上的 minma​​x 负载,按 countryCode 排序?

.countryCode 'count of number of servers' 'lowest .load' 'highest .load'
AU    5    13%       30%
UK   10    35%       88%
US   25    12%       50%

等等……

我相信 group_by 的使用,或者它与 map、set path、get path 和 count 的组合可能会起作用,但我希望有更高技能的人可以帮助我。

每个 json 条目如下所示:


[
  {
    "country": "Albania",
    "**countryCode**": "AL",
    "region": "Europe",
    "regionCode": "EU",
    "**load**": 12,
    "id": "4c333aa2-d08b-4d11-9073-61c351ce1c1e",
    "coordinates": {
      "longitude": 19.8188889,
      "latitude": 41.3275
    },
    "info": [
      {
        "id": "55994450-0c87-4df3-b585-3836c67a863d",
        "entry": {
          "value": "U2FsdGVkX1/VX5IKc3H3ruClgbw7JgnqW7Eacumx8aM="
        }
      }
    ],
    "type": "generic",
    "location": "Tirana",
    "**connectionName**": "al-tia.prod.surfshark.com",

从引用一些可能命令的另一篇文章中摘录

'map(.Properties.ItmId)
| reduce .[] as $i (
    {}; setpath([$i]; getpath([$i]) + 1)
  )
| to_entries | .[] | { "ItemId": .key, "Count": .value }'

【问题讨论】:

    标签: json curl group-by count jq


    【解决方案1】:

    您可以先执行group_by,然后从那里派生其余字段

    group_by(.countryCode) 
      | map([ .[0].countryCode, length, min_by(.load).load, max_by(.load).load])
    

    编辑:删除了@peak 指出的冗余排序

    您可以选择添加.[] | @tsv 以获取问题中的表结构

    【讨论】:

    • group_by 进行排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2019-07-07
    • 1970-01-01
    相关资源
    最近更新 更多