解析多层 json 拼成二维表
【问题】
Is there any way to convert multiple JSONs into one csv ?
My JSON file is like this:
{
“Title” : {
“name” :“ABC”,
“id” :“1”,
“job”:“Teacher”
},
“Circle”:{
“area”:“2R”
},
“Triangle”:{
“length”:“45”
}
}
If you will see, this JSON has 3 roots and different elements under each root. How to convert this JSON to CSV so that it can be opened in excel and can be viewed as follows:
Title
Name ABC
id 1
job Teacher
Circle
area 2r
Triangle
length 45
Can someone please suggest?
【回答】
将多层的 json 拼成二维表,java 针对二维表的方法不够丰富,硬编码麻烦,不如用 SPL 实现,再被 java 调用
A | ||
---|---|---|
1 | =file(“d:\\data.json”).read() | |
2 | =json(A1) | |
3 | =A2.fno().(join@p((f=A2.field(~)).fname():key;f.array():value)) | |
4 | =A3.(~.record@i([A2.fname(#),null],1)) | |
5 | =A4.conj() | |
6 | =file(“D:\\result.csv”).export@c(A5) | |
运行结果:
A1:读取 json 返回字符串
A2:将字符串解析成序表
A3:合并字段名和字段值
A4:缺省处插入 null 值
A5:拆分记录进行并集
A6:输出到 csv