如何对 jq 中的映射数组中的值求和?

How do I sum the values in an array of maps in jq?

给定以下形式的 JSON 流:

{ "a": 10, "b": 11 } { "a": 20, "b": 21 } { "a": 30, "b": 31 }

我想将每个对象中的值相加并输出一个对象,即:

{ "a": 60, "b": 63 }

我猜这可能需要将上面的对象列表展平为 [name, value] 对的数组,然后使用 reduce 对值求和,但是使用语法的文档 reduce 很悲哀。

除非你的 jq 有 inputs,否则你将不得不使用 -s 标志来吸取对象。然后你将不得不做大量的操作:

  1. 每个对象都需要映射到 key/value 对
  2. 将这些对展平为一个数组
  3. 按键对进行分组
  4. 映射出每个组,将值累积到单个 key/value 对
  5. 将对映射回一个对象
map(to_entries)
    | add
    | group_by(.key)
    | map({
          key: .[0].key,
          value: map(.value) | add
      })
    | from_entries

在 jq 1.5 中,这可能会得到很大的改进:您可以取消 slurping,直接阅读 inputs

$ jq -n '
reduce (inputs | to_entries[]) as {$key,$value} ({}; .[$key] += $value)
' input.json

由于我们只是简单地累加每个对象中的所有值,因此仅 运行 通过所有输入的 key/value 对,并将它们全部相加会更容易.

另一种很好地说明 jq 强大功能的方法是使用名为 "sum" 的过滤器,定义如下:

def sum(f): reduce .[] as $row (0; . + ($row|f) );

为了解决手头的特定问题,可以使用上面提到的 -s (--slurp) 选项,以及表达式:

{"a": sum(.a), "b": sum(.b) }  # (2)

标有(2)的表达式只计算两个指定的和,但很容易概括,例如如下:

# Produce an object with the same keys as the first object in the 
# input array, but with values equal to the sum of the corresponding
# values in all the objects.
def sumByKey:
  . as $in
  | reduce (.[0] | keys)[] as $key
    ( {}; . + {($key): ($in | sum(.[$key]))})
;

我在列出 GitHub 中的所有工件时遇到了同样的问题(有关详细信息,请参阅 here)并想对它们的大小求和。

curl https://api.github.com/repos/:owner/:repo/actions/artifacts \
     -H "Accept: application/vnd.github.v3+json" \
     -H "Authorization:  token <your_pat_here>" \
     | jq '.artifacts | map(.size_in_bytes) | add'

输入:

{
  "total_count": 3,
  "artifacts": [
    {
      "id": 0000001,
      "node_id": "MDg6QXJ0aWZhY3QyNzUxNjI1",
      "name": "artifact-1",
      "size_in_bytes": 1,
      "url": "https://api.github.com/repos/:owner/:repo/actions/artifacts/2751625",
      "archive_download_url": "https://api.github.com/repos/:owner/:repo/actions/artifacts/2751625/zip",
      "expired": false,
      "created_at": "2020-03-10T18:21:23Z",
      "updated_at": "2020-03-10T18:21:24Z"
    },
    {
      "id": 0000002,
      "node_id": "MDg6QXJ0aWZhY3QyNzUxNjI0",
      "name": "artifact-2",
      "size_in_bytes": 2,
      "url": "https://api.github.com/repos/:owner/:repo/actions/artifacts/2751624",
      "archive_download_url": "https://api.github.com/repos/:owner/:repo/actions/artifacts/2751624/zip",
      "expired": false,
      "created_at": "2020-03-10T18:21:23Z",
      "updated_at": "2020-03-10T18:21:24Z"
    },
    {
      "id": 0000003,
      "node_id": "MDg6QXJ0aWZhY3QyNzI3NTk1",
      "name": "artifact-3",
      "size_in_bytes": 3,
      "url": "https://api.github.com/repos/docker/mercury-ui/actions/artifacts/2727595",
      "archive_download_url": "https://api.github.com/repos/:owner/:repo/actions/artifacts/2727595/zip",
      "expired": false,
      "created_at": "2020-03-10T08:46:08Z",
      "updated_at": "2020-03-10T08:46:09Z"
    }
  ]
}

输出:

6