json

Convert CSV to Json using Java

1. Introduction

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A CSV file typically stores tabular data (numbers and text) in plain text, in which case each line will have the same number of fields. The CSV file format is not fully standardized. Separating fields with commas is the foundation, but commas in the data or embedded line breaks have to be handled specially. Some implementations disallow such content while others surround the field with quotation marks, which yet again creates the need for escaping if quotation marks are present in the data.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

2. Setup

In this section, we will see how to set up our java project. We will use the org.json.json library for our purpose. The files in this package implement JSON encoders/decoders in Java. It also includes the capability to convert between JSON and XML, HTTP headers, Cookies, and CDL. Create a maven project and include the below dependency in your project:

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20220320</version>
    </dependency>
</dependencies>

Your pom file will look something like the below:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JavaCodeGeeks</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220320</version>
        </dependency>
    </dependencies>

</project>

3. CSV to JSON

In this section, we will see how to convert a CSV file to JSON. First, we will create a test CSV file in the resource folder. The resource folder is already in the classpath so it’s easy to access the files inside it. Below is the sample CSV file which we will convert to JSON:

input.csv

First name, Surname, Gender, Date of Birth
Tom, Cruise, Male, 01-01-1966
Shahrukh, Khan, Male, 02-02-1955

As you can see that this file is very simple and only contains two records. The first line is the header and second onwards we got data. Now let us start writing the Java code to read this file and then convert it to JSON.

First, let us read this file into a stream:

InputStream inputStream = CsvToJson.class.getClassLoader().getResourceAsStream("input.csv");

The getResourceAsStream() method returns an input stream for reading the specified resource(in our case the csv file).

Now let us read this stream and convert it into a string which we need to construct our JSON file. First, let’s create a BufferedReader from the given input stream:

new BufferedReader(new InputStreamReader(inputStream))

The BufferedReader class is used to read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made by a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly.

Now let us call the lines() method on the above stream. It returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read-only occurs during the terminal stream operation.

new BufferedReader(new InputStreamReader(inputStream)).lines()

Now let us collect these and join them with a new-line character:

String csvAsString = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));

The collect() method performs a mutable reduction operation on the elements of this stream using a Collector.

Now we will use the org.json.CDL class to convert this string representation of CSV to JSON:

String json = CDL.toJSONArray(csvAsString).toString();

This produces a JSONArray of JSONObjects from a comma-delimited text string, using the first row as a source of names.

Now we need to write this to a file. To do that we will use the Files.write().

Files.write(Path.of("src/main/resources/output.json"), json.getBytes(StandardCharsets.UTF_8));

Below is the full source code for the java file:

CsvToJson.java

package org.javacodegeeks;

import org.json.CDL;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

public class CsvToJson {

    public static void main(String[] args) {
        InputStream inputStream = CsvToJson.class.getClassLoader().getResourceAsStream("input.csv");
        String csvAsString = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
        String json = CDL.toJSONArray(csvAsString).toString();
        try {
            Files.write(Path.of("src/main/resources/output.json"), json.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now if you run this class a file (output.json) will be created in the src/main/resources folder. The content will be similar to the below:

output.json

[
  {
    "First name": "Tom",
    "Date of Birth": "01-01-1966",
    "Gender": "Male",
    "Surname": "Cruise"
  },
  {
    "First name": "Shahrukh",
    "Date of Birth": "02-02-1955",
    "Gender": "Male",
    "Surname": "Khan"
  }
]

4. JSON to CSV

In this section, we will see how to convert a JSON file to CSV format. First, we will create a sample input.json file in the resources folder:

input.json

[
  {
    "First name": "John",
    "Date of Birth": "10-11-1999",
    "Gender": "Male",
    "Surname": "Dickson"
  },
  {
    "First name": "Milena",
    "Date of Birth": "20-04-1975",
    "Gender": "Female",
    "Surname": "Trump"
  }
]

Now let us look at the java code to convert this JSON file to CSV. First, we will read the file as we did before:

InputStream inputStream = JsonToCsv.class.getClassLoader().getResourceAsStream("input.json");

Now we will convert this stream to JSONArray object:

JSONArray jsonArray = new JSONArray(new JSONTokener(inputStream));

A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and JSONArray constructors to parse JSON source strings. Now we will use CDL.toString() method which produces a comma delimited text from a JSONArrayObjects. The first row will be a list of names obtained by inspecting the first JSONObject. Now let us write this in a CSV file:

Files.write(Path.of("src/main/resources/output.csv"), CDL.toString(jsonArray).getBytes(StandardCharsets.UTF_8));

Below is the full source code of converting a JSON file to CSV format:

JsonToCsv.java

package org.javacodegeeks;

import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class JsonToCsv {

    public static void main(String[] args) {
        InputStream inputStream = JsonToCsv.class.getClassLoader().getResourceAsStream("input.json");
        JSONArray jsonArray = new JSONArray(new JSONTokener(inputStream));
        try {
            Files.write(Path.of("src/main/resources/output.csv"), CDL.toString(jsonArray).getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5. Summary

In this article, we looked at what are CSV and JSON files and then how to convert them. First, we looked at a simple way of converting a CSV file to a JSON format and then vice-versa. Please note that there are lot more libraries that we can use to achieve the same result but I find org.json.json quite easy and straightforward. Also, this example is for a very simple case – If the input file is very big then we can’t read the whole file in memory and we need to rethink our solution.

6. Download

This was an example of converting a CSV file to JSON and vice-versa in java.

Download
You can download the full source code of this example here: Convert CSV to Json using Java

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button