If you really want to pass in a JSON array with the keys whose values you want to extract, you could do that like so
jq -r --argjson keys '["f3", "f1"]' '.attributes[$keys[]]' file.json
Given the data in the question, this would return
three
one
To get these on the same line with a space delimiter:
jq -r --argjson keys '["f3", "f1"]' '[.attributes[$keys[]]] | join(" ")' file.json
or, by simply passing the output of the first command to paste -d ' ' -s -
:
jq -r --argjson keys '["f3", "f1"]' '.attributes[$keys[]]' file.json |
paste -d ' ' -s -
It would be more convenient for the user of the command to be able to mention the fields they want without having to create a JSON array (potentially having to remember to encode the keys properly):
jq -r '.attributes[$ARGS.positional[]]' file.json --args f3 f1
Or, for the space-delimited single-line output variant:
jq -r '.attributes[$ARGS.positional[]]' file.json --args f3 f1 |
paste -d ' ' -s -
Note that --args
and the subsequent arguments need to be last on the command line.