将随机 json 转换为 csv

Convert a random json to csv

我有一个由内部进程输出的 json 文件。格式不像典型的 json。我需要将其转换为平面 csv。

{"id":"321321","date1":"01APR2021","date2":"01APR2021","date3":"25MAY2021","numField1":0,"Status":"Success","strField1":null,"listField":[{"name":"val1","version":"v1.1","value":122,"str1":"this is another text","str2":"","str3":"","str4":"","str5":"","str6":"this is a text"},{"name":"val2","version":"v1.1","value":-789,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":""},{"name":"val3","version":"v1.1","value":123,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":""},{"name":"val4","version":"v1.1","value":234,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":"and yet another text"}]}
{"id":"345626","date1":"02APR2021","date2":"02APR2021","date3":"25MAY2021","numField1":0,"Status":"Success","strField1":null,"listField":[{"name":"val1","version":"v1.1","value":588,"str1":"this is another text in row2","str2":"","str3":"","str4":"","str5":"","str6":"this is a text"},{"name":"val2","version":"v1.1","value":189,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":""},{"name":"val3","version":"v1.1","value":521,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":""},{"name":"val4","version":"v1.1","value":453,"str1":"","str2":"","str3":"","str4":"","str5":"","str6":"and yet another text"}]}

到目前为止我已经尝试了两种不同的方法:

cat test | jq -r "[ .id, .date1, .date2, .date3, .numField1, .Status, .strField1 ] | @csv"
>>
"321321","01APR2021","01APR2021","25MAY2021",0,"Success",
"345626","02APR2021","02APR2021","25MAY2021",0,"Success",

cat test | jq -r ".listField | .[] | flatten | @csv"
>>
"val1","v1.1",122,"this is another text","","","","","this is a text"
"val2","v1.1",-789,"","","","","",""
"val3","v1.1",123,"","","","","",""
"val4","v1.1",234,"","","","","","and yet another text"
"val1","v1.1",588,"this is another text in row2","","","","","this is a text"
"val2","v1.1",189,"","","","","",""
"val3","v1.1",521,"","","","","",""
"val4","v1.1",453,"","","","","","and yet another text"

我需要两种格式的输出:

  1. 对第二个输出中的每一行重复第一个代码的输出以获得“长格式数据”。
"321321","01APR2021","01APR2021","25MAY2021",0,"Success","val1","v1.1",122,"this is another text","","","","","this is a text"
"321321","01APR2021","01APR2021","25MAY2021",0,"Success","val2","v1.1",-789,"","","","","",""
"321321","01APR2021","01APR2021","25MAY2021",0,"Success","val3","v1.1",123,"","","","","",""
"321321","01APR2021","01APR2021","25MAY2021",0,"Success","val4","v1.1",234,"","","","","","and yet another text"
"345626","02APR2021","02APR2021","25MAY2021",0,"Success","val1","v1.1",588,"this is another text in row2","","","","","this is a text"
"345626","02APR2021","02APR2021","25MAY2021",0,"Success","val2","v1.1",189,"","","","","",""
"345626","02APR2021","02APR2021","25MAY2021",0,"Success","val3","v1.1",521,"","","","","",""
"345626","02APR2021","02APR2021","25MAY2021",0,"Success","val4","v1.1",453,"","","","","","and yet another text"
  1. 以固定顺序将第二个输出的 4 行连接成一行以获得“宽格式数据”。
"321321","01APR2021","01APR2021","25MAY2021",0,"Success","val1","v1.1",122,"this is another text","","","","","this is a text","val2","v1.1",-789,"","","","","","","val3","v1.1",123,"","","","","","","val4","v1.1",234,"","","","","","and yet another text"
"345626","02APR2021","02APR2021","25MAY2021",0,"Success","val1","v1.1",588,"this is another text in row2","","","","","this is a text","val2","v1.1",189,"","","","","","","val3","v1.1",521,"","","","","","","val4","v1.1",453,"","","","","","and yet another text"

假设 .listField 中的所有对象都是共形的:

第一个任务

[ .id, .date1, .date2, .date3, .numField1, .Status, .strField1 ]
+ (.listField[] | [.[]] )
| @csv

第二个任务

[ .id, .date1, .date2, .date3, .numField1, .Status, .strField1 ]
+ [.listField[] | .[] ]
| @csv

我会尽可能避免扁平化,因为它往往会隐藏惊喜。