-1

I am new to jq, I have a JSON file from (call: all.2):

{"code":0, "data": {"date": 1617978312600,"ticker": {"KAVAUSDT": {"vol": "15022.96236047","low": "7.0419","open":"7.8990","high": "8.1997","last": "7.1516","buy": "7.1516","buy_amount":"11.02402741","sell":"7.1699","sell_amount": "37.46319593"},"CFXUSDT": {"vol": "108550.36674746","low": "1.226636","open": "1.267724","high": "1.378400","last": "1.273187","buy": "1.272200","buy_amount": "14.65071234","sell": "1.294900","sell_amount": "84.88000789"},"SUSHIBTC": {"vol": "5644.10352324","low": "0.00024680","open": "0.00024857","high": "0.00026493","last": "0.00025704","buy": "0.00025563","buy_amount": "104.74291100","sell": "0.00025730","sell_amount": "94.16400000"},"SUNUSDT": {"vol": "7058.81491709","low":"37.3431","open":"37.9590","high": "44.0446","last": "40.6945","buy": "40.1001","buy_amount": "25.40000000","sell":"41.0771","sell_amount": "3.40000000"}}}, "message": "OK"}

I want to convert the data to csv ( space separated) similar to the following: As Shown in the ATTACHED IMAGE

I tried the following, but it didnt work:

mlr --j2c cat all.2>allq1.csv

jq -r '.[]|[ .ticker,.ticker.vol,.ticker.low,.ticker.open,.ticker.high,.ticker.last,.ticker.buy,.ticker.buy_amount,.ticker.sell,.ticker.sell_amount ]|@csv' all.2 > all.csv
  • Welcome to SO. As per the [mcve] guidelines, it would be helpful if you could include the text of the expected output to facilitate verification of putative solutions. An image, though possibly helpful in some ways, is insufficient for this purpose. – peak Apr 13 '21 at 13:49

1 Answers1

1

Try

jq -r '.data.ticker | to_entries[] | [.key, (.value | (.vol|tonumber), (.low|tonumber))] | @csv'

Explanation: .data.ticker - path to your data, to_entries[] - since ticker name is key, we need to convert this object to .key and .value, () - parenthesis helps, so you don't need to write full path every time, tonumber - convert string to number (remove double quotes)

Add all your fields

lojza
  • 1,823
  • 2
  • 13
  • 23