Here are some additional explanations.
For the input object {"key1":1, "key2":2, "key3":3} I would like to delete all keys that are not in the set of desired keys ["key1","key3","key4"]
jq -n --argjson desired_keys '["key1","key3","key4"]' \ --argjson input '{"key1":1, "key2":2, "key3":3}' \ ' $input | with_entries( select( .key == ($desired_keys[]) ) )'
with_entries converts {"key1":1, "key2":2, "key3":3} to the next array of key value pairs and displays the select statement in the array, and then returns the resulting array back to the object.
Here is the internal object in the with_entries .
[ { "key": "key1", "value": 1 }, { "key": "key2", "value": 2 }, { "key": "key3", "value": 3 } ]
we can select keys from this array that match our criteria.
The magic happens here ... look at what happens in the middle of this team. The following command takes an extended array of values ββand turns them into a list of objects that we can select.
jq -cn '{"key":"key1","value":1}, {"key":"key2","value":2}, {"key":"key3","value":3} | select(.key == ("key1", "key3", "key4"))'
This will produce the following result:
{"key":"key1","value":1} {"key":"key3","value":3}
A recording command may be a little complicated, but itβs easy to remember that it accepts a filter and is defined as follows
def with_entries(f): to_entries|map(f)|from_entries;
This is the same as
def with_entries(f): [to_entries[] | f] | from_entries;
Another part of the question that confuses people is the multiple matches on the right ==
Consider the following command. We see that the output is an external production of all left lists and right lists.
jq -cn '1,2,3| . == (1,1,3)' true true false false false false false false true
If this predicate is in the select statement, we save the input when the predicate is true. Please note that you can also duplicate input.
jq -cn '1,2,3| select(. == (1,1,3))' 1 1 3