我想加入数组'.fileStats'和'.files',以便只有一个数组。
初始JSON:
{"arguments":{"torrents":[{"fileStats":[{"bytesCompleted":0,"priority":0,"wanted":false},{"bytesCompleted":0,"priority":0,"wanted":true},{"bytesCompleted":0,"priority":0,"wanted":true}],"files":[{"bytesCompleted":0,"length":30,"name":"1.txt"},{"bytesCompleted":0,"length":99,"name":"2.exe"},{"bytesCompleted":0,"length":4833317512,"name":"3.iso"}]}]},"result":"success"}
合并数组后的对象示例:
{
"bytesCompleted": 0,
"length": 30,
"name": "1.txt",
"priority": 0,
"wanted": true
}
我通过“展平”采用了另一种方法:
.arguments.torrents[] | map(to_entries) | flatten | group_by(.key)
但是更希望能够基于'.wanted'为是/否来选择对象,然后检索'.name'。
答案 0 :(得分:0)
jq '.arguments.torrents[] | range(.fileStats|length) as $i| select(.fileStats[$i].wanted)| .fileStats[$i]+.files[$i]'
我认为上面的代码可以满足您的需求。您的输入将提供以下输出。
{
"bytesCompleted": 0,
"priority": 0,
"wanted": true,
"length": 99,
"name": "2.exe"
}
{
"bytesCompleted": 0,
"priority": 0,
"wanted": true,
"length": 4833317512,
"name": "3.iso"
}
有关更多详细信息,请在jq手册中查找“变量”,“范围”,“长度”,“选择”和“合并”。 https://stedolan.github.io/jq/manual/