到目前为止,我以前将标准 JSON 转换为 CSV 时没有遇到任何问题jq
。但现在我的数据变得复杂了。
主要基础标签是account
和subaccount
。所有其他值都是变量。条目year
具有可变值,并且经常随时间而变化。这是我无法解决的任务。
输出如下
{
"jsonrpc": "2.0",
"result": {
"current": [{
"number": 171883808,
"commission": 10,
"year": [
[1999, 9224, 0],
[2000, 41919, 9224],
[2001, 162945, 41919],
[2002, 397993, 162945],
[2003, 751570, 397993],
[2004, 886466, 751570]
],
"status": true,
"last": 9782473,
"account": "VFUIJOPQW",
"subaccount": "BLPORDGS"
}, {
"number": 69999012,
"commission": 15,
"year": [
[2012, 97587, 0],
[2013, 472685, 97587],
[2014, 605963, 472685],
[2015, 698634, 605963],
[2016, 1931094, 1745922]
],
"status": true,
"last": 9782490,
"account": "VFUIJOXXX",
"subaccount": "BLPORXXX"
}],
"id": 1
}
}
答案1
我假设您的意思是输出一个带有account
和subaccount
的year
表。您可以使用更新赋值运算符 ( |=
) 来执行此操作,例如:
jq -r '.result | .current | .[] |
.account as $account |
.subaccount as $subaccount |
.year[] |= ([$account, $subaccount] + .) |
.year[] | @tsv'
输出:
VFUIJOPQW BLPORDGS 1999 9224 0
VFUIJOPQW BLPORDGS 2000 41919 9224
VFUIJOPQW BLPORDGS 2001 162945 41919
VFUIJOPQW BLPORDGS 2002 397993 162945
VFUIJOPQW BLPORDGS 2003 751570 397993
VFUIJOPQW BLPORDGS 2004 886466 751570
VFUIJOXXX BLPORXXX 2012 97587 0
VFUIJOXXX BLPORXXX 2013 472685 97587
VFUIJOXXX BLPORXXX 2014 605963 472685
VFUIJOXXX BLPORXXX 2015 698634 605963
VFUIJOXXX BLPORXXX 2016 1931094 1745922