[英]Shell Script Assigning json object to an variable for iteration
DATA="$(curl "https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=BA&apikey=demo" | jq '.bestMatches' | jq '.[] | ."1. symbol"')"
我想遍歷 DATA 變量,理想情況下它是一個數組。 我想在這里做的是兩方面的。
{
"bestMatches": [
{
"1. symbol": "BA",
"2. name": "The Boeing Company",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "1.0000"
},
{
"1. symbol": "BAC",
"2. name": "Bank of America Corporation",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.8000"
},
{
"1. symbol": "BABA",
"2. name": "Alibaba Group Holding Limited",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.6667"
},
{
"1. symbol": "GOLD",
"2. name": "Barrick Gold Corporation",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.5714"
},
{
"1. symbol": "BIDU",
"2. name": "Baidu Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.5000"
},
{
"1. symbol": "BAYRY",
"2. name": "Bayer Aktiengesellschaft",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.4000"
},
{
"1. symbol": "BLDP",
"2. name": "Ballard Power Systems Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.3333"
},
{
"1. symbol": "BHC",
"2. name": "Bausch Health Companies Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.3333"
},
{
"1. symbol": "BK",
"2. name": "The Bank of New York Mellon Corporation",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.1538"
}
]
}
上面是我得到的json object。
使用 tonumber 等將字段值從 integer 轉換為數字:
readarray data <<< "$(jq -r '.bestMatches[] | select(."9. matchScore" | tonumber > 0.5) | ."1. symbol"' jq)"
將jq命令的output重定向到readarray中創建數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.