如何通过JQ乘法结果从json导出CSV文件 [英] How can I export CSV file from json with the result of multiplication via JQ

查看:41
本文介绍了如何通过JQ乘法结果从json导出CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的json

{"lastUpdateId":18891938,
"bids":[
    ["3.23600000","101.76600000"],
    ["3.23500000","2161.63600000"],
    ["3.23100000","30.95000000"],
    ["3.23000000","3.12600000"],
    ["3.22900000","303.95100000"]
],
"asks":[
    ["3.24100000","15.90200000"],
    ["3.24200000","1679.00000000"],
    ["3.24500000","953.98800000"],
    ["3.24800000","7.57700000"],
    ["3.25400000","37.26700000"]
]}

然后我想将它导出到 CSV,并得到每一行的乘法结果(即第一行应该是这样的)

Then I would like to export it to CSV with the result of multiplication for every rows(i.e. 1st row should be something like this)

bids"、3.23600000"、101.76600000"、329.314776"<- 这是根据 3.23600000 * 101.76600000 计算的

"bids","3.23600000","101.76600000","329.314776" <- this is calculated from 3.23600000 * 101.76600000

现在,下面的代码导出第一个数字和第二个数字,但是它们相乘的结果.

now, the code below export 1st numbers and 2nd number but the result of their multiplication.

jq -r keys[] as $k | select(.[$k]|type=="array") |[$k]+.[$k][]|@csv

如何在每一行的末尾一起导出乘法的结果?

How can I export the result of multiplication together in the end of each line?

https://jqplay.org/s/8zMbHxlfoz

推荐答案

你只需要在jq程序中添加一行:

You only need add one line to the jq program:

keys[] as $k 
| select(.[$k]|type=="array") 
| [$k]+.[$k][]
| .[length]= (.[-2:]|map(tonumber)|.[0]*.[1])
|@csv

你可能希望加入一个 tostring.

You might wish to throw in a tostring.

这篇关于如何通过JQ乘法结果从json导出CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆