How to convert CSV file to JSON file in Java using Jackson

This tutorial shows how to convert CSV file to JSON file using Jackson.

1) Dependencies

Add the following highlighted dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                      http://maven.apache.org/maven-v4_0_0.xsd">
	<modelversion>4.0.0</modelversion>
	<groupid>com.kalliphant.jackson.example</groupid>
	<artifactid>CSV2JSON</artifactid>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>CSV2JSON</name>

	<dependencies>
	   <dependency>
	     <groupid>com.fasterxml.jackson.core</groupid>
	     <artifactid>jackson-databind</artifactid>
	     <version>2.8.9</version>
	   </dependency>
	   <dependency>
	     <groupid>com.fasterxml.jackson.dataformat</groupid>
	     <artifactid>jackson-dataformat-csv</artifactid>
	     <version>2.8.9</version>
	   </dependency>		
	</dependencies>
</project>

2) Java Class

This example reads the CSV data from the input.csv file and write the JSON data to output.json file.

package com.kalliphant.jackson.example;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class CSV2JSON {

	public static void main(String[] args) throws Exception {
		File input = new File("input.csv");
		File output = new File("output.json");

		CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
		CsvMapper csvMapper = new CsvMapper();

		// Read data from CSV file
		List<object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();

		ObjectMapper mapper = new ObjectMapper();

		// Write JSON formated data to output.json file
		mapper.writerWithDefaultPrettyPrinter().writeValue(output, readAll);

		// Write JSON formated data to stdout
		System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(readAll));
	}
}

3) input.json file contents

name,year,genres, runtime
Back to the Future,1985,Adventure | Comedy | Sci-Fi,116 min
The Godfather,1972,Crime | Drama,2h 55min

4) Run the java class

When you run the above java class, it will create output.json with the following contents

[ {
  "name" : "Back to the Future",
  "year" : "1985",
  "genres" : "Adventure | Comedy | Sci-Fi",
  "runtime" : "116 min"
}, {
  "name" : "The Godfather",
  "year" : "1972",
  "genres" : "Crime | Drama",
  "runtime" : "2h 55min"
} ]

5) Console output

[ {
  "name" : "Back to the Future",
  "year" : "1985",
  "genres" : "Adventure | Comedy | Sci-Fi",
  "runtime" : "116 min"
}, {
  "name" : "The Godfather",
  "year" : "1972",
  "genres" : "Crime | Drama",
  "runtime" : "2h 55min"
} ]

NOTE: Above example pretty formats the JSON data. If you want to print JSON data in one line use
mapper.writexxxx(..) method instead of mapper.writerWithDefaultPrettyPrinter().writexxxx(..)