Last active
May 31, 2021 12:03
-
-
Save arnaudroger/7cbb9ca1acda66341fc10bf54ab01439 to your computer and use it in GitHub Desktop.
How to Convert CSV to JSON in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.fasterxml.jackson.core.JsonFactory; | |
| import com.fasterxml.jackson.core.JsonGenerator; | |
| import org.simpleflatmapper.csv.CsvParser; | |
| import org.simpleflatmapper.csv.CsvReader; | |
| import java.io.IOException; | |
| import java.util.Iterator; | |
| // uses http://simpleflatmapper.org/0101-getting-started-csv.html for csv parser | |
| // and jackson-core for JsonGenerator | |
| // alternative to https://dzone.com/articles/how-to-convert-csv-to-json-in-java | |
| // stream csv as we read it | |
| // see https://gist.github.com/arnaudroger/cf7806de83766b51dbfe84a1fab559b0 for reduce garbage version | |
| public class CsvToJson { | |
| public static void main(String[] args) throws IOException { | |
| CsvReader reader = CsvParser.reader("col1,col2\nval1,val2"); | |
| JsonFactory jsonFactory = new JsonFactory(); | |
| Iterator<String[]> iterator = reader.iterator(); | |
| String[] headers = iterator.next(); | |
| try (JsonGenerator jsonGenerator = jsonFactory.createGenerator(System.out)) { | |
| jsonGenerator.writeStartArray(); | |
| while (iterator.hasNext()) { | |
| jsonGenerator.writeStartObject(); | |
| String[] values = iterator.next(); | |
| int nbCells = Math.min(values.length, headers.length); | |
| for(int i = 0; i < nbCells; i++) { | |
| jsonGenerator.writeFieldName(headers[i]); | |
| jsonGenerator.writeString(values[i]); | |
| } | |
| jsonGenerator.writeEndObject(); | |
| } | |
| jsonGenerator.writeEndArray(); | |
| } | |
| } | |
| } |
usinhagroveco
commented
Oct 29, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment