我有一个JSON Blob。它包含要处理的项目队列。我正在使用jq对其进行过滤。此队列中的每个对象都有键downloadId
。我可以使用此过滤器返回downloadId
具有特定值的对象列表:
.[] | select((.downloadId) == "ABCA01479FCAB77D0A8A5C499D5ABAA147E97B47")
但是我真正需要的是一个布尔值,如果队列中有多个对象具有该值true
,它将返回downloadId
。我一直在看jq手册,但是我不太确定该怎么做。我可以尝试使用any
any(.[]; .downloadId == "ABCA01479FCAB77D0A8A5C499D5ABAA147E97B47")
,但是如果该值有一个实例,但不超过一个,则返回true。
这是一个斑点:
[
{
"sizeleft": 805679928.0,
"timeleft": "00:00:00",
"estimatedCompletionTime": "2019-05-03T17:30:31.370856Z",
"status": "Paused",
"trackedDownloadStatus": "Ok",
"statusMessages": [],
"downloadId": "1C2DE46A5B4258BE7AC47FEFAE71432897417126",
"id": 1065601863
},
{
"sizeleft": 836459293.0,
"timeleft": "00:00:00",
"estimatedCompletionTime": "2019-05-03T17:30:31.370864Z",
"status": "Paused",
"trackedDownloadStatus": "Ok",
"statusMessages": [],
"downloadId": "728084A13FD172FED437C6AE503A8CF4A8D317AE",
"id": 1177597916
},
{
"sizeleft": 836459293.0,
"timeleft": "00:00:00",
"estimatedCompletionTime": "2019-05-03T17:30:31.370864Z",
"status": "Paused",
"trackedDownloadStatus": "Ok",
"statusMessages": [],
"downloadId": "ABCA01479FCAB77D0A8A5C499D5ABAA147E97B47",
"id": 1177597916
}
]
答案 0 :(得分:3)
要获得有效的解决方案,让我们定义repeated/2
来检查给定值是否在流中多次出现:
def repeated(stream; $value):
label $done
| foreach stream as $x (0;
if $x == $value then . + 1 else . end;
if . == 2 then true, break $done else empty end)
// false;
当然,这里的关键是一旦第二次遇到该值,便会跳出循环。
使用上述方法,解决方案将是:
repeated(.[] | .downloadId; "ABCA01479FCAB77D0A8A5C499D5ABAA147E97B47")