-
-
Save arnaudroger/7cbb9ca1acda66341fc10bf54ab01439 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} |
thanks arnaudroger, this is good stuff, well done!
Hello @arnaudroger , I wanted to use your code but I have a problem to import the fasterxml and the simpleflatmapper... Do you know why ? I tried to look on the web to find answer but I found nothing...
Thank you for your answer, Naig.
@naigldo do you have an error message? there is not conflict as far as I know between those 2, sfm has been built to not required any external dep.
Yes, this is the same error message for those two " cannot resolve symbole 'fasterxml' " and same for sfm. The solution proposed by IntelliJ is to add and external JAR but when it look for one it doesn't find anything.
what build are you using maven or an idea projecT?
Maven I think
have you added the dependency for your pom.xml? have you reimport the project in idea?
How can get this output as json array for manipulate?
@osoluche in this example it writes direclty to the writer, so not really any manipulation possible there. though it would node be difficult to create a JsonNode structure instead and do some transformation after.
@arnaudroger, thank you, i've found the solution, just export the writer, and create a new json object from writer.toString( ), then i can manipulate it, for a loop;
use the following import statements to resolve any issue .
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.simpleflatmapper.csv.CsvParser;
import org.simpleflatmapper.lightningcsv.CloseableCsvReader;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
Pom Details ,
<dependency>
<groupId>org.simpleflatmapper</groupId>
<artifactId>sfm-csv</artifactId>
<version>8.2.3</version>
</dependency>
<dependency>
<groupId>org.simpleflatmapper</groupId>
<artifactId>lightning-csv</artifactId>
<version>8.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.0-rc1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.0-rc1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-csv -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>org.simpleflatmapper</groupId>
<artifactId>sfm-csv</artifactId>
<version>8.2.3</version>
</dependency>
Note that it's possible to do better by bypassing the String and array creation using the CellConsumer callback.
see https://gist.github.com/arnaudroger/cf7806de83766b51dbfe84a1fab559b0