select only the arrays that
contain the given value
$val, then pulling out the keys of those arrays:
jq -r --argjson val 2 'map_values(select(contains([$val]))) | keys' file
contains() returns true if the given array (
[$val]) is fully contained in the input to the function. If
contains() returns true for an array in the input,
select() will return that array.
map_values() to perform this selection of the arrays associated with the keys in the input object. This will give me a slimmed-down object with the arrays that pass the selection, and passing the result to
keys will then give me an array of keys, which I unpack into a set of loose strings with the final
For the given input data, with 2 as the value of
$val, this returns
--argjson rather than
--arg to pass the value 2 into the expression as
$val as the data is numeric, not strings.