无法通过 jq 将 JSON 转换为 csv
Cant convert JSON to csv by jq
我是 jq 的新手,我有一个来自(调用:all.2)的 JSON 文件:
{"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"}
我想将数据转换为 csv(space 分隔),类似于以下内容:
As Shown in the ATTACHED IMAGE
我尝试了以下方法,但没有用:
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
尝试
jq -r '.data.ticker | to_entries[] | [.key, (.value | (.vol|tonumber), (.low|tonumber))] | @csv'
说明:.data.ticker
- 数据路径,to_entries[]
- 由于代码名称是关键,我们需要将此对象转换为 .key
和 .value
,()
- 括号有帮助,所以你不需要每次都写完整路径,tonumber
- 将字符串转换为数字(删除双引号)
添加所有字段
我是 jq 的新手,我有一个来自(调用:all.2)的 JSON 文件:
{"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"}
我想将数据转换为 csv(space 分隔),类似于以下内容: As Shown in the ATTACHED IMAGE
我尝试了以下方法,但没有用:
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
尝试
jq -r '.data.ticker | to_entries[] | [.key, (.value | (.vol|tonumber), (.low|tonumber))] | @csv'
说明:.data.ticker
- 数据路径,to_entries[]
- 由于代码名称是关键,我们需要将此对象转换为 .key
和 .value
,()
- 括号有帮助,所以你不需要每次都写完整路径,tonumber
- 将字符串转换为数字(删除双引号)
添加所有字段