0

I need help converting a bunch of json strings to CSVs inside a loop

for (MyBusinessObj obj:MyBusinessObjects){
String json = convertBOToJson();
Need to add this to CSV (only one header row)
}

Each string looks like below:

String1 {"name":"Ram","Age":"80",".....}
String2  {"name":"Rick","Age":"67",".....}

I need a CSV like:

name    age
Ram     80
Rick    67

Each string can be close to 50MB, and I could have millions of rows. so I cannot construct one giant JSON string so I can get a JSONArray. How do I add these strings to a CSV file?

I have to use base Java, or some common library like openCSV

Nila
  • 71
  • 6
  • 1
    https://stackoverflow.com/questions/20388119/converting-json-to-csv https://stackoverflow.com/questions/7172158/converting-json-to-xls-csv-in-java – Zukaru Feb 16 '23 at 03:34
  • thank you, but the link given below works on a jsonarray. I dont have a json array, but each of array elements in a different string. I cant construct such an array as mine could be hige and cant cross 1Gb – Nila Feb 16 '23 at 04:07
  • Why first convert from MyBusinessObj (BO) to JSON and then from JSON to CSV? – life888888 Feb 16 '23 at 11:05
  • 1
    Example code, direct write obj to csv ( https://dirask.com/posts/Java-how-to-write-java-object-to-CSV-file-using-Jackson-CSV-library-8pVk0j ) – life888888 Feb 16 '23 at 11:17
  • life888888 because MyBO contains a huge XML Blob of variable size, so I dont know what the columns are coming out of each of these Blobs from a DB with millions of rows. So I parse it out into a JSON, then flatten it and then create a CSV out of it. Could not find a simpler way to directly convert this XML to CSV. or directly blob to CSV. – Nila Feb 16 '23 at 15:19
  • life888888 thanks for the reference link. Will try that out and will post my comments here – Nila Feb 16 '23 at 15:20

0 Answers0