Say I have data something like this:
{
"18" : [ 2, 3, 3 ],
"28" : [ 2, 2, 7 ],
"45" : [ 3, 3, 5 ]
}
I'd like to make a jq
query that returns the key/keys of the object with the array that contains a given value. For example, 2 exists in keys "18" and "28", and 7 exists only exists in "28". It is the name of the parent object I'm interested in finding. I've been through the manual and I can't seem to find an example of anything like this.
map_values
is doing. The query cannot be run without it:jq -r --argjson val 2 'select(contains([$val]))' test.json
results injq: error (at test.json:4): object ({"18":[2,3,...) and array ([2]) cannot have their containment checked
. And the manual formap_values
isn't what I expect at all. – A. Que Jun 21 '22 at 12:32map_values()
works likemap()
, but takes an object as input rather than an array. The expression thatmap_values()
evaluates is evaluated on the values in the object. In the answer above, I'm doing aselect()
, which effectively removes the values that we don't want to keep. – Kusalananda Jun 21 '22 at 12:39