Curl

jq 無法索引錯誤

  • September 23, 2020

我正在嘗試使用 jq 從該端點獲取某些資訊

curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '[.]'

和下面的輸出

[
 {
   "success": true,
   "message": "",
   "result": [
     {
       "MarketName": "BTC-1ST",
       "High": 0.00010322,
       "Low": 9.32e-05,
       "Volume": 1475820.53114847,
       "Last": 9.414e-05,
       "BaseVolume": 145.89904728,
       "TimeStamp": "2017-10-05T00:32:45.283",
       "Bid": 9.415e-05,
       "Ask": 9.521e-05,
       "OpenBuyOrders": 614,
       "OpenSellOrders": 5887,
       "PrevDay": 0.00010169,
       "Created": "2017-06-06T01:22:35.727"
     },
     {
       "MarketName": "BTC-2GIVE",
       "High": 1.31e-06,
       "Low": 1.24e-06,
       "Volume": 4356547.69360079,
       "Last": 1.29e-06,
       "BaseVolume": 5.59000303,
       "TimeStamp": "2017-10-05T00:21:46.333",
       "Bid": 1.29e-06,
       "Ask": 1.31e-06,
       "OpenBuyOrders": 298,
       "OpenSellOrders": 2290,
       "PrevDay": 1.29e-06,
       "Created": "2016-05-16T06:44:15.287"
     },
     {
       "MarketName": "BTC-ABY",
       "High": 1.89e-06,
       "Low": 1.62e-06,
       "Volume": 31422008.3611497,
       "Last": 1.68e-06,
       "BaseVolume": 53.99330434,
       "TimeStamp": "2017-10-05T00:25:21.307",
       "Bid": 1.68e-06,
       "Ask": 1.7e-06,
       "OpenBuyOrders": 437,
       "OpenSellOrders": 4761,
       "PrevDay": 1.63e-06,
       "Created": "2014-10-31T01:43:25.743"
     }

當我試圖只得到MarketName

curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.[] | select(.MarketName=="BTC-1ST")'

我收到以下錯誤

jq: error (at <stdin>:0): Cannot index boolean with string "MarketName"

其他錯誤

curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.[1]'

我明白了

jq: error (at <stdin>:0): Cannot index object with number

任何人都知道獲得這些的正確命令嗎?

如果您想要{}與該市場名稱匹配的所有內容,您可以執行

$ curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.result[] | select(.MarketName=="BTC-1ST")'
{
 "MarketName": "BTC-1ST",
 "High": 0.00010322,
 "Low": 8.402e-05,
 "Volume": 1820113.98502106,
 "Last": 8.799e-05,
 "BaseVolume": 175.00575508,
 "TimeStamp": "2017-10-05T03:40:26.463",
 "Bid": 8.799e-05,
 "Ask": 8.817e-05,
 "OpenBuyOrders": 329,
 "OpenSellOrders": 6316,
 "PrevDay": 0.00010101,
 "Created": "2017-06-06T01:22:35.727"
}

如果你想列出你可以做的所有 MarketNames

$ curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.result[].MarketName'
"BTC-1ST"
"BTC-2GIVE"
"BTC-ABY"
....

還必須程式語言列表從 0 開始。所以如果我想在 jq 中獲取列表中的第一項,我可以這樣做

jq '.[0]'

只有 1 個列表項,這就是為什麼如果您使用會出現錯誤的原因[1]

引用自:https://serverfault.com/questions/876944