A Simple Way of Parsing & Writing JSON Data into a CSV File
【Question】
I am trying to create a CSV file from a dynamically generated JSON array. For example:
“People” :
[{“name” : “Bob”, “age” : 5},
{“dob” : “5/2/4”, “name” : “Alice”},
{“name” : “George”}]
would create the csv file
name, age, dob
Bob, 5,
Alice, , 5/2/4
George, ,
As you can see, I need to create a column for every field in the JSON array. The order of each key-value pair in the JSON array is random. I also do not know name of the keys beforehand. Don’t worry about the “People” field, there will only be one array in each JSON. (This will most be the CSV name).
I have tried using theJackson CSV library but this requires me to create a pre-defined POJO as a middle tier (JSON -> POJO -> CSV). As far as I know, Java does not allow me to dynamically generate a class’ member variables at run time, so this method will not work. Same with GSON.
I will need to do this over a large data set, so efficiency would be pretty key. What would be the best way to approach this problem?
【Answer】
It’s complicated to parse JSON arrays with random-ordered key-value pairs in Java. But it’s simple to do it in SPL (Structured Process Language), as shown below:
A |
||
1 |
=file(“d:\\data.json”).read() |
|
2 |
=json(A1).People |
|
3 |
=file(“D:\\result.csv”).export@c(A2) |
The result of executing the SPL script:
A1: Read in the JSON array as a table sequence;
A2: Retrieve the table sequence;
A3: Write data in the table sequence to a CSV file.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL