jq - How to select objects based on a 'blacklist' of property values

冷暖自知 提交于 2021-02-04 20:57:32

问题


Similar to the question answered here: jq - How to select objects based on a 'whitelist' of property values, I'd like to select objects based on a blacklist of property values...

The following works fine as a whitelist: 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!"
}

The problem is, I want to negate that and only show commits from authors besides 'stedolan' and 'dtolnay'; however, if I use != or not, I seem to get the same wrong result:

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"

Any suggestions?


回答1:


One solution would simply be to use index with not:

.[] | .author.login | select( . as $i | $blacklist | index($i) | not)

However, assuming your jq has all/2, there is something to be said for using it:

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

If your jq does not have it, then there is still something to be said for using this solution, with all/2 defined as follows:

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


来源:https://stackoverflow.com/questions/40141310/jq-how-to-select-objects-based-on-a-blacklist-of-property-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!