I'm trying to do something akin to this:
jq -r '. | ."Time Series (Daily)"."2020-12-02" | ."1. open"' newdata.json
...but with the key coming from a variable, as in:
jq -r --arg key "$key" '. | ."Time Series (Daily)"."[$key]" | ."1. open"' newdata.json
The first one works just fine, but when I assign the date to a variable called key and then try to get the data, it fails.
I tried This answer and This answer. But did not work for me.
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "AB",
"3. Last Refreshed": "2020-12-02",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-12-02": {
"1. open": "32.6700",
"2. high": "33.3300",
"3. low": "32.5000",
"4. close": "33.1200",
"5. volume": "273799"
},
"2020-12-01": {
"1. open": "32.1500",
"2. high": "32.8000",
"3. low": "32.0000",
"4. close": "32.6000",
"5. volume": "265086"
},
"2020-11-30": {
"1. open": "32.3800",
"2. high": "32.4900",
"3. low": "31.7500",
"4. close": "31.8700",
"5. volume": "251970"
}
}
}
The above is the newdata.json file. What I want to get is the "1. open" value. I am using a for loop to iterate over all the keys of "Time Series (Daily)" and the keys are generated correctly. There is no issue with that. I then want to use the $key variable in each iteration to get the data I need.
readarray keys <<< "$(jq '."Time Series (Daily)" | keys[]' newdata.json)"
for key in "${keys[@]}"; do
jq -r --arg key "$key" '. | ."Time Series (Daily)" | .[$key] | ."1. open"' newdata.json
done