jq unique_by-选择剩余的元素 [英] jq unique_by - Choose remaining element
问题描述
我目前正在尝试通过REST API从PagerDuty获取事件列表,
I'm currently trying to get a list of incidents from PagerDuty via the REST api, which returns them in JSON array. I want to remove any duplicate events by using unique_by() on the incident_key. However, I want the first occurrence of the incident_key, and unique_by() is removing all but the last. Right now, if I have incident_number 849, 850, and 851, all with the same incident_key, unique_by() will return 851.
简单的例子:
[
{ "reference_key":"200", "id":"1" },
{ "reference_key":"200", "id":"2" },
{ "reference_key":"200", "id":"3" },
{ "reference_key":"201", "id":"4" },
{ "reference_key":"201", "id":"5" },
{ "reference_key":"201", "id":"6" }
]
我想做的是根据ID使用unique_by()来获得reference_key的首次出现.所以在这种情况下,我希望输出为
What I'm trying to do is use unique_by() to get the first occurrence of reference_key, based on the id. So in this case, I'd want the output to be
[
{ "reference_key":"200", "id":"1" },
{ "reference_key":"201", "id":"4" }
]
问题是我对此无能为力,而对于我目前正在尝试使用的数据,它返回的是最后一次而不是第一次的返回.
The problem is that I have no control over this, and with the data I'm currently trying to do this with, it is returning the last occurrence instead of the first, like so.
[
{ "reference_key":"200", "id":"3" },
{ "reference_key":"201", "id":"6" }
]
我尝试过使用反向,然后调用unique_by(),但是得到的结果是相同的.有什么办法可以对此进行控制吗?
I have tried using reverse and then calling unique_by(), but I am getting the same results. Is there any way to have some control over this?
推荐答案
也许您的jq版本不够新.使用jq 1.5:
Maybe your version of jq is not sufficiently recent. Using jq 1.5:
unique_by( .reference_key )
收益
[{"reference_key":"200","id":"1"},{"reference_key":"201","id":"4"}]
(截至2016年1月18日(7835a72),内置的sort
过滤器是稳定的;在此之前,稳定性取决于平台.)
(As of January 18, 2016 (7835a72), the builtin sort
filter is stable; prior to that, stability was platform-dependent.)
如果您没有访问足够新版本的jq的权限,请考虑以下内容,这些版本已通过jq 1.3、1.4和1.5进行了测试:
If you don't have access to a sufficiently recent version of jq, then consider the following, which has been tested with jq 1.3, 1.4 and 1.5:
def bucketize(f):
reduce .[] as $x ({}; .[$x|f] += [$x] );
bucketize(.reference_key) | .[][0]
或更经济:
reduce .[] as $x ({};
$x.reference_key as $key
| if .[$key] then . else .[$key] = $x end)
| .[]
这篇关于jq unique_by-选择剩余的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!