I have a JSON array in this format
{
"id": 707860,
"name": "Hurzuf",
"country": "UA",
"coord": {
"lon": 34.283333,
"lat": 44.549999
}
}
I am using jq to parse it. I'm trying to extract the values of only the keys which are provided in a BASH variable. I tried this with one key in the variable and it works.
KEY=id
jq -r --arg key $KEY '.[] | .[$key]'
However when I tried the same with KEY=coord.lat the output is null and I'm stumped when the number of keys isn't a given.
For example, if the variable is
KEYS=id,name
then the output should be
707860,"Hurzuf"
or, if the variable is
KEYS=coord.lon,coord.lat
then the output should be
34.283333,44.549999
How can this be done?