How to Convert CSV to JSON in Java

Convert CSV to JSON using Jackson. Use a POJO for conversion or a List & Map for intermediate storage.

“Any fool can know. The point is to understand.”
― Albert Einstein

 

1. Introduction

CSV to JSON conversion is easy. In this article, we present a couple of methods to parse CSV data and convert it to JSON. The first method defines a POJO and uses simple string splitting to convert CSV data to POJO, which in turn is serialized to JSON. The second method uses a more complete CSV parser with support for quoted fields and commas embedded within fields. In this method, we use the Java Collection classes to store the parsed data and convert those to JSON.

We use Jackson for the JSON conversion.

2. Simple CSV Parsing

When the CSV file to be parsed is simple that there are no quoted fields or commas embedded in fields, you can use a simple pattern matcher to split the CSV fields.

Pattern pattern = Pattern.compile(",");

3. POJO Definition

We are going to parse the CSV data and create the POJO (Plain-Old-Java-Object) shown in this definition.

public class Player
{
    private int year;
    private String teamId;
    private String leagueId;
    private String playerId;
    private int salary;

    public Player(int year,
                  String teamId,
                  String leagueId,
                  String playerId,
                  int salary)
    {
        this.year = year;
        this.teamId = teamId;
        this.leagueId = leagueId;
        this.playerId = playerId;
        this.salary = salary;
    }

    // getters and setters here
};

4. Reading the CSV Data

Open the CSV file using a BufferedReader in a try-with-resources block.

try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
    // processing code here
}

Create the List of POJOs using a streaming pipeline. The first line is skipped since it is the CSV header. The line is split into fields using the Pattern, converted to appropriate types and used to create the object.

List<Player> players = in
    .lines()
    .skip(1)
    .map(line -> {
            String[] x = pattern.split(line);
            return new Player(Integer.parseInt(x[0]),
                              x[1],
                              x[2],
                              x[3],
                              Integer.parseInt(x[4]));
        })
    .collect(Collectors.toList());

5. Serialize to JSON

Once the List is ready, use Jackson’s ObjectMapper to write the JSON. Check this guide for full details on JSON serialization and deserialization.

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValue(System.out, players);

And here is the whole programs segment.

Pattern pattern = Pattern.compile(",");
try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
    List<Player> players = in
        .lines()
        .skip(1)
        .map(line -> {
                String[] x = pattern.split(line);
                return new Player(Integer.parseInt(x[0]),
                                  x[1],
                                  x[2],
                                  x[3],
                                  Integer.parseInt(x[4]));
            })
        .collect(Collectors.toList());
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.writeValue(System.out, players);
}

6. CSV to JSON Conversion – no POJO

In this example, we use the CSV parser presented in this article. This CSV parser can handle multi-line quoted fields and commas embedded within fields just like Excel can. It uses the first line of the CSV file as field names and loads the data into a List which is then exported to JSON.

Here is the complete segment.

try (InputStream in = new FileInputStream(csvFile);) {
    CSV csv = new CSV(true, ',', in);
    List<String> fieldNames = null;
    if ( csv.hasNext() ) fieldNames = new ArrayList<>(csv.next());
    List<Map<String,String>> list = new ArrayList<>();
    while (csv.hasNext()) {
        List<String> x = csv.next();
        Map<String,String> obj = new LinkedHashMap<>();
        for (int i = 0 ; i < fieldNames.size() ; i++) {
            obj.put(fieldNames.get(i), x.get(i));
        }
        list.add(obj);
    }
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.writeValue(System.out, list);
}

Here is a part of the CSV data that was converted:

rep_file_num,CIK,entity_name,street1,street2,city,state_code,zip,filing_date,doc_type_code
814-00034,0000731812,SIERRA RESOURCES CORP,629 J STREET,SUITE 202,SACRAMENTO,CA,95814,12/30/96,15
814-00053,0000821472,WESTFORD TECHNOLOGY VENTURES LP,17 ACADEMY ST 5TH FLOOR,[NULL],NEWARK,NJ,07102-2905,01/28/04,NO ACT
...
814-00098,0000878932,"EQUUS TOTAL RETURN, INC.",EIGHT GREENWAY PLAZA,SUITE 930,HOUSTON,TX,77046,08/25/16,40-APP/A

Notice that one of the fields in the data is quoted because of embedded commas. Here is the converted JSON for that record which shows that the record has been parsed and converted correctly.

{
 "rep_file_num" : "814-00098",
 "CIK" : "0000878932",
 "entity_name" : "EQUUS TOTAL RETURN, INC.",
 "street1" : "EIGHT GREENWAY PLAZA",
 "street2" : "SUITE 930",
 "city" : "HOUSTON",
 "state_code" : "TX",
 "zip" : "77046",
 "filing_date" : "08/25/16",
 "doc_type_code" : "40-APP/A"
}

Summary

CSV data can be converted to JSON via a POJO using Jackson. If a POJO is not already defined or required, you can always use the Java Collection classes to store parsed data and later convert it to JSON.