JSON to CSV with Jackson

JSON to CSV with Jackson

Needed to convert some JSON output to CSV this week on one of my REST endpoints. Previously on this endpoint I was using Gson to do the conversion. Gson does not natively support JSON to CSV conversion. So though I love how easy it is for me to type:

[code language=”java”]
Gson gson = new Gson();

gson.toJson("Hello World");
[/code]

and easily retrtieve my Json. I switched over to using Jackson because it does support CSV conversion of my Pojos as well. When trying to figure this out the following links came in very handy:

stackoverflow

cowtowncoder

jackson-csv

Since the post from cowtowncoder is a bit old I figured I’d write a new blogpost in case someone else runs into this problem.

[code language=”xml”]<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>datadidit.helpful.hints</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>json-to-csv</artifactId>
<name>json-to-csv</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
[/code]

Note: Also took the time to try out JUnit 5.

Made a simple POJO for converting to JSON and CSV:

[code language=”java”]

package datadidit.helpful.hints.csv;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"firstName", "lastName", "dob"})
public class Simple {
private String firstName;

private String lastName;

private Date dob;

public Simple(){
}

public Simple(String firstName, String lastName, Date dob) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getDob() {
return dob;
}

public void setDob(Date dob) {
this.dob = dob;
}
}

[/code]

Then wrote a Unit test for testing the code out:

[code language=”java”]

package datadidit.helpful.hints.csv;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

public class JsonToCsvTest {
private ObjectMapper mapper;

private CsvMapper csvMapper;

@BeforeEach
public void setup(){
csvMapper = new CsvMapper();
mapper = new ObjectMapper();
}
@Test
public void CreateJson() throws JsonProcessingException{
List<Simple> simpleList = new ArrayList<>();
simpleList.add(new Simple("hello", "world", new Date()));

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(simpleList);

System.out.println(json);

System.out.println("Now as CSV: ");
CsvSchema schema = csvMapper.schemaFor(Simple.class).withHeader();
System.out.println(csvMapper.writer(schema).writeValueAsString(simpleList));
}
}

[/code]

Output as JSON:

[code language=”text”]

[ {
"firstName" : "hello",
"lastName" : "world",
"dob" : 1468293742743
} ]

[/code]

Output as CSV:

[code language=”text”]

firstName,lastName,dob
hello,world,1468075423437

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *