使用 jq 获取 json 数据的第一个元素

Getting the first element of json data with jq

我正在与 Poloniex 合作 API。使用returnTicker函数时,数据是这样的

curl "https://poloniex.com/public?command=returnTicker"

{
  "BTC_BTS": {
    "id": 14,
    "last": "0.00000111",
    "lowestAsk": "0.00000112",
    "highestBid": "0.00000110",
    "percentChange": "0.09900990",
    "baseVolume": "3.12079869",
    "quoteVolume": "2318738.79293715",
    "isFrozen": "0",
    "high24hr": "0.00000152",
    "low24hr": "0.00000098"
  },
  "BTC_DASH": {
    "id": 24,
    "last": "0.00466173",
    "lowestAsk": "0.00466008",
    "highestBid": "0.00464358",
    "percentChange": "0.02318430",
    "baseVolume": "1.98111396",
    "quoteVolume": "425.22973220",
    "isFrozen": "0",
    "high24hr": "0.00482962",
    "low24hr": "0.00450482"
....

  },
  "USDT_GRT": {
    "id": 497,
    "last": "0.72811272",
    "lowestAsk": "0.75999916",
    "highestBid": "0.72740000",
    "percentChange": "0.48594450",
    "baseVolume": "133995.43411815",
    "quoteVolume": "194721.36672887",
    "isFrozen": "0",
    "high24hr": "0.79000000",
    "low24hr": "0.45000020"
  },
  "TRX_SUN": {
    "id": 498,
    "last": "500.00000000",
    "lowestAsk": "449.99999999",
    "highestBid": "100.00000000",
    "percentChange": "0.00000000",
    "baseVolume": "0.00000000",
    "quoteVolume": "0.00000000",
    "isFrozen": "0",
    "high24hr": "0.00000000",
    "low24hr": "0.00000000"
  }
}  


我想要这样的输出

BTC_BTS : 14 : 0.00000111 : 0.00000112 : 0.00000110 : 0.09900990 : 3.12079869 : 2318738.79293715 : 0 : 0.00000152 : 0.00000098
...
USDT_GRT : 497 : 0.72428700 : 0.75999958 : 0.72630001 : 0.47813685 : 133968.74968533 : 194695.96886712 : 0 : 0.79000000 : 0.45000020
TRX_SUN : 498 : 500.00000000 : 449.99999999 : 100.00000000 : 0.00000000 : 0.00000000 : 0.00000000 : 0 : 0.00000000 : 0.00000000

我正在使用 jq,我的问题是访问货币对名称。

我可以做到;

14 : 0.00000111 : 0.00000112 : 0.00000110 : 0.09900990 : 3.12079869 : 2318738.79293715 : 0 : 0.00000152 : 0.00000098
...
497 : 0.72428700 : 0.75999958 : 0.72630001 : 0.47813685 : 133968.74968533 : 194695.96886712 : 0 : 0.79000000 : 0.45000020
498 : 500.00000000 : 449.99999999 : 100.00000000 : 0.00000000 : 0.00000000 : 0.00000000 : 0 : 0.00000000 : 0.00000000

通过使用这个命令;

curl "https://poloniex.com/public?command=returnTicker" |jq -r | jq '.[] | (.id|tostring) + " : " + (.last|tostring) + " : " + (.lowestAsk|tostring) + " : " + (.highestBid|tostring) + " : " + (.percentChange|tostring) + " : " + (.baseVolume|tostring) + " : " + (.quoteVolume|tostring) + " : " + (.isFrozen|tostring) + " : " + (.high24hr|tostring) + " : " + (.low24hr|tostring)'|jq -r

不仅如此,在每个jq管道中,我都无法访问json

的第一个元素

我不是指 |jq .BTC_BTS|jq .USDT_GRT 管道。

|jq . 给出整个 json,|jq .[] 给出第一个元素之后的子元素。

如何访问第一个路径?

顺便说一句,我可能用jq写了又蠢又长的流水线。如果您有任何想法将整个 json 转换为行-列数据,我愿意接受您的想法。

谢谢大家的回答。

我想这就是你想要的。

curl -s "https://poloniex.com/public?command=returnTicker" | \
jq -r 'to_entries
       | .[] 
       | [ .key, (.value | to_entries | .[] | .value) ] 
       | join(" : ")'

简而言之,将所有内容放入数组中并使用 join 生成所需的输出。

更新

如 luciole75w 所述,我的解决方案步骤太多。这样比较好。

jq -r 'to_entries[] | [ .key, .value[] ] | join(" : ")'

也就是说,我会使用 peak 的解决方案。我的不保证每行的列都相同。

为了安全起见,最好不要假设键的顺序在所有内部对象中都是相同的。因此:

keys_unsorted as $outer
| (.[$outer[0]] | keys_unsorted) as $keys
| $outer[] as $k
| [ $k, .[$k][$keys[]] ]
| join(" : ")