给定以下形式的 JSON 流:
{ "a": 10, "b": 11 } { "a": 20, "b": 21 } { "a": 30, "b": 31 }
我想对每个对象中的值求和并输出一个对象,即:
{ "a": 60, "b": 63 }
我猜这可能需要将上面的对象列表展平成一个对数组,[name, value]
然后使用对这些值求和,reduce
但是使用语法的文档reduce
是可悲的。
除非您的 jq 有inputs
,否则您将不得不使用-s
标志来啜饮对象。然后你必须做大量的操作:
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
由于我们只是累加每个对象中的所有值,因此只需遍历所有输入的键/值对并将它们全部相加会更容易。
在列出来自 GitHub 的所有工件时,我遇到了同样的问题(有关详细信息,请参见此处)并想总结它们的大小。
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
另一种很好地说明 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]))})
;