jq - 如何根据属性值的“黑名单”选择对象

时间:2016-10-19 20:51:24

标签: json select jq blacklist

与此处回答的问题类似:jq - How to select objects based on a 'whitelist' of property values,我想根据属性值黑名单选择对象......

以下作为白名单工作正常:curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson whitelist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $whitelist[]) | {author: .author.login, message: .commit.message}'

{
  "author": "dtolnay",
  "message": "Remove David from maintainers"
}
{
  "author": "stedolan",
  "message": "Make jv_sort stable regardless of qsort details."
}
{
  "author": "stedolan",
  "message": "Add AppVeyor badge to README.md\n\nThanks @JanSchulz, @nicowilliams!"
}

问题是,我想否定这一点,除了'stedolan'和'dtolnay'之外,只显示作者的承诺;但是,如果我使用!=not,我似乎得到了相同的错误结果:

nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $blacklist[] | not) | .author.login' | sort | uniq -c | sort -nr
     14 "nicowilliams"
      2 "stedolan"
      1 "dtolnay"
nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login != $blacklist[]) | .author.login' | sort | uniq -c | sort -nr
     14 "nicowilliams"
      2 "stedolan"
      1 "dtolnay"

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

一种解决方案就是将indexnot

一起使用
.[] | .author.login | select( . as $i | $blacklist | index($i) | not)

但是,假设你的jq有all/2,那么使用它有一些说法:

.[] | .author.login | select( . as $i | all($blacklist[]; $i != .))

如果您的jq没有,那么使用此解决方案仍有一些问题,all/2的定义如下:

def all(s; condition): reduce s as $i (true; . and ($i | condition));