How to read and write Java object to JSON file?

The following example demonstrate how to serialize and deserialize Java object to JSON file. The Jackson’s ObjectMapper class provides writeValue(File, Object) and readValue(File, Class<T>) methods which allow us to write an object into JSON file and read JSON file into an object respectively.

package org.kodejava.example.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class ObjectToJsonFile {
    public static void main(String[] args) {
        Artist artist = new Artist();
        artist.setId(1L);
        artist.setName("The Beatles");

        ObjectMapper mapper = new ObjectMapper();

        File file = new File("artist.json");
        try {
            // Serialize Java object info JSON file.
            mapper.writeValue(file, artist);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // Deserialize JSON file into Java object.
            Artist newArtist = mapper.readValue(file, Artist.class);
            System.out.println("newArtist.getId() = " + newArtist.getId());
            System.out.println("newArtist.getName() = " + newArtist.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The result of the code snippet are:

newArtist.getId() = 1
newArtist.getName() = The Beatles

Maven Dependencies

<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

How to convert JSON string to Java object?

In the following example we will convert JSON string to Java object using ObjectMapper class from the Jackson library. This class provides a method readValue(String, Class<T>) which will deserialize a JSON string into Java object. The first argument to the method is the JSON string and the second parameter is the result type of the conversion.

package org.kodejava.example.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonToObject {
    public static void main(String[] args) {
        String json = "{\"id\": 1, \"name\": \"The Beatles\"}";

        ObjectMapper mapper = new ObjectMapper();
        try {
            Artist artist = mapper.readValue(json, Artist.class);
            System.out.println("Artist = " + artist);

            System.out.println("artist.getId() = " + artist.getId());
            System.out.println("artist.getName() = " + artist.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The code snippet above will print to following output:

Artist = Artist{id=1, name='The Beatles'}
artist.getId() = 1
artist.getName() = The Beatles

Maven Dependencies

<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

How to convert Java object to JSON string?

The following example shows how to convert Java object into JSON string using Jackson. Jackson provide ObjectMapper class provides functionality to read and write JSON data. The writeValueAsString(Object) method to serialize any Java object into string.

package org.kodejava.example.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectToJson {
    public static void main(String[] args) {
        Artist artist = new Artist();
        artist.setId(1L);
        artist.setName("The Beatles");

        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(artist);
            System.out.println("JSON = " + json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Running the code snippet above will print out the following result:

JSON = {"id":1,"name":"The Beatles"}

The structure of Artist class.

package org.kodejava.example.jackson;

public class Artist {
    private Long id;
    private String name;

    public Artist() {
    }

    public Artist(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    // Getters & Setters
}

Maven Dependencies

<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.8.6/jackson-core-2.8.6.jar -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.6</version>
</dependency>
<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.8.6/jackson-annotations-2.8.6.jar -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.6</version>
</dependency>
<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>