Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there an open source java library to convert a CSV (or XLS) file to a JSON object? I tried using json.cdl (http://bethecoder.com/applications/tutorials/json/json-java/csv-to-jsonarray.html) but somehow it does not seem to work for large CSS strings. I'm trying to find something like http://www.cparker15.com/code/utilities/csv-to-json/, but written in java. Any help is appreciated. Thanks!

share|improve this question
possible duplicate of convert csv/xls to json – StriplingWarrior Mar 1 '12 at 21:15
possibly not an exact duplicate since it asks specifically for a Java solution. – DNA Mar 1 '12 at 21:22
add comment (requires an account with 50 reputation)

2 Answers

You can use Open CSV to map CSV to a Java Bean, and then use JAXB to convert the Java Bean into a JSON object.

http://opencsv.sourceforge.net/#javabean-integration

http://jaxb.java.net/guide/Mapping_your_favorite_class.html

share|improve this answer
add comment (requires an account with 50 reputation)

If your CSV is simple, then this is easy to write by hand - but CSV can include nasty edge cases with quoting, missing values, etc.

  • load the file using BufferedReader.readLine()
  • use String.split(",") to get the value from each line
  • write each value to the output using BufferedWriter
    • with the necessary JSON braces and quoting

You might want to use a CSV library, then convert to JSON 'by hand'

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.