在jq中将数组和字典连接到TSV中



我在底部有输入的JSON数据。

我想生成一个TSV输出,如下所示。<TAB>是TAB字符。我如何在jq中做到这一点?

timestamp<TAB>open<TAB>high<TAB>dividends
1623072600<TAB>4229.33984375<TAB>4232.33984375<TAB>
1623159000<TAB>4233.81005859375<TAB>4236.740234375<TAB>
1623245400<TAB>4232.990234375<TAB>4237.08984375<TAB>0.42
1623331800<TAB>4228.56005859375<TAB>4249.740234375<TAB>
1623418200<TAB>4242.89990234375<TAB>4248.3798828125<TAB>
{
"timestamp": [
1623072600,
1623159000,
1623245400,
1623331800,
1623418200
],
"indicators": {
"quote": [
{
"open": [
4229.33984375,
4233.81005859375,
4232.990234375,
4228.56005859375,
4242.89990234375
],
"high": [
4232.33984375,
4236.740234375,
4237.08984375,
4249.740234375,
4248.3798828125
]
}
]
},
"events": {
"dividends": {
"1623245400": {
"amount": 0.42,
"date": 1623245400
}
}
}
}

使用带有-r命令行选项的jq:

(.events.dividends|map_values(.amount)) as $dividends
| ["timestamp", "open", "high", "dividends"],
( [.timestamp, (.indicators.quote[0] | .open, .high),
[$dividends[.timestamp[]|tostring]]]
| transpose[])
| @tsv

请注意股息列是如何使用提供的$precipatedictionary计算的:

$dividends[.timestamp[]|tostring]

最新更新