user284274 user284274 - 1 month ago 8
JSON Question

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

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?

Answer

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));