0

I am trying to convert JSON file from the command below to CSV format:

curl -X GET 'https://api.coinex.com/v1/market/kline?market=BCHBTC&type=1min' -H "Accept: application/json"

I tried jq command as following, but it was unsuccessful :

jq '.data |to_entries[] | @csv'

OR

jq '.data |to_entries[] | [.key,  (.0|tonumber),(.1|tonumber),(.2|tonumber),(.3|tonumber),(.4|tonumber),(.5|tonumber),(.6|tonumber),(.7|tonumber)   )] | @csv'  

desired output is like:

            1619094720  0.01738857  0.01742868  0.01742868  0.01737360  1.24151689  0.0215974650849251  BCHBTC
            1619094780  0.01742823  0.01742913  0.01742913  0.01742807  0.89060000  0.0155215977170000  BCHBTC
            1619094840  0.01744941  0.01745423  0.01745423  0.01744941  0.42820000  0.0074724290140000  BCHBTC
            1619094900  0.01745458  0.01740857  0.01745458  0.01740857  0.73530000  0.0128245470890000  BCHBTC
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55

2 Answers2

1

There is no need for to_entries, just apply the filter directly to the arrays, e.g.:

jq -r '.data[] | .[0:-1] | map(tonumber) | @tsv'

Note that the above removes the "BCHBTC" text field, as noted by peak in the comments this can be included with the special or operator (//):

jq -r '.data[] | tonumber? // . | @tsv'
cigien
  • 57,834
  • 11
  • 73
  • 112
Thor
  • 45,082
  • 11
  • 119
  • 130
1

jq -r '.data[] | @tsv ' solved the problem, thanks @ Inian

  • Please don't add "thanks" as answers. They don't actually provide an answer to the question, and can be perceived as noise by its future visitors. Once you [earn](https://meta.stackexchange.com/questions/146472/what-is-the-best-way-to-increase-my-reputation-and-privileges) enough [reputation](https://stackoverflow.com/help/whats-reputation), you will gain privileges to [upvote answers](https://stackoverflow.com/help/privileges/vote-up) you like. This way future visitors of the question will see a higher vote count on that answer, and the answerer will also be rewarded with reputation points. – Tyler2P Dec 04 '21 at 22:37