简体   繁体   中英

Getting the first element of json data with jq

I'm working with Poloniex API. While using returnTicker function, the data comes like this.

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"
  }
}  


I want the output like this

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

I am using jq and my problem is accesing the currency pair name.

I could do this;

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

by using this command;

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

not only this, in every jq pipeline, I cant access the first element of json

I am not meaning the |jq.BTC_BTS or |jq.USDT_GRT pipeline.

|jq. gives whole json, |jq.[] gives the sub elements after the first element.

How can i access the first path?

By the way, I may have written stupid and long pipeline with jq. If you have any idea to convert whole json to a row-column data, I am open to your ideas.

Thank you all for your answers.

To be safe, it might be better not to assume that the ordering of the keys is the same in all the inner objects. Ergo:

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

I think this does what you want.

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

In a nutshell, put everything in an array and use join to produce the desired output.

Update

As luciole75w notes, my solution has too many steps. This is better.

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

That said, I would use peak's solution. Mine does not guarantee that the columns are the same for each line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM