Skip to content

Instantly share code, notes, and snippets.

@arnaudroger
Last active May 31, 2021 12:03
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arnaudroger/7cbb9ca1acda66341fc10bf54ab01439 to your computer and use it in GitHub Desktop.
Save arnaudroger/7cbb9ca1acda66341fc10bf54ab01439 to your computer and use it in GitHub Desktop.
How to Convert CSV to JSON in Java
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();
}
}
}
@arnaudroger
Copy link
Author

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

@gmillsPredix
Copy link

thanks arnaudroger, this is good stuff, well done!

@naigldo
Copy link

naigldo commented May 16, 2019

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.

@arnaudroger
Copy link
Author

@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.

@naigldo
Copy link

naigldo commented May 16, 2019

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.

@arnaudroger
Copy link
Author

what build are you using maven or an idea projecT?

@naigldo
Copy link

naigldo commented May 16, 2019

Maven I think

@arnaudroger
Copy link
Author

have you added the dependency for your pom.xml? have you reimport the project in idea?

@osoluche
Copy link

osoluche commented Nov 7, 2019

How can get this output as json array for manipulate?

@arnaudroger
Copy link
Author

@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.

@osoluche
Copy link

osoluche commented Nov 7, 2019

@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;

@susilkumar18
Copy link

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>

@usinhagroveco
Copy link

	<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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment