0

I'm trying to convert the json object into a CSV file using java.

My json :

{"Type":"Camera","modelName":"Alpha","asinno":"B0YUSAWZ"} 

Expected O/p:

Type,modelName,asinno
Camera,Alpha,B0YUSAWZ

I have tried using "CDL.toString()", but toString is expecting a json array but not a simple json object. How can we deal with simple json ?

Can anyone please help me out...

Dherik
  • 17,757
  • 11
  • 115
  • 164
fervent
  • 123
  • 1
  • 2
  • 10

1 Answers1

0

JSON isn't really a (CSV) table format. So I guess you'll have to come up with something yourself. (If you don't already have,) you can gain Object-access to your JSON. You'll also need a CSVWriter. You can take the OpenCSV library or write a simple one yourself (that just concats the strings with commas inbetween).

Here is some logic

csvWriter = new CSVWriter(...)
json = parseJson(...)

//header line
for each key in json
    csvWriter.write(key)

csvWriter.nextLine()

//value line
for each key in json
    value = json[key]
    csvWriter.write(value)

csvString = csvWriter.asString()
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52