Read JSON Strings into Java Objects with Jackson API

A guide to using Jackson Databind API to Map JSON Strings into Java Objects. It covers examples of converting JSON String to Custom Java Objects and Java Maps.

Setup

Let’s get the Jackson Dependency and a POJO ready to run examples.

Jackson Dependency

Use the latest Jackson version in your dependency.

pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>{version}</version>
</dependency>Code language: HTML, XML (xml)

Or build.gradle

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '{version}'Code language: Gradle (gradle)

Create a POJO

We will create a StudentDto and use it to map JSON strings into

package com.amitph.spring.tutorials.students.model;

public class StudentDto {
    private long id;
    private String firstName;
    private String lastName;
    private int joinedYear;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    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 int getJoinedYear() {
        return joinedYear;
    }

    public void setJoinedYear(int joinedYear) {
        this.joinedYear = joinedYear;
    }
}Code language: Java (java)

JSON to Java Objects using Jackson

To read JSON String and Map into Java Object, we can use the readValue method. We need to provide the JSON string and a target class.

private static void jsonToObject() throws JsonProcessingException {
    String json = """
            {
                "id":1111,
                "firstName":"Strong",
                "lastName":"Belwas",
                "joinedYear":2022
            }
            """
    ObjectMapper objectMapper = new ObjectMapper();
    StudentDto studentDto = objectMapper.readValue(json, StudentDto.class);
    System.out.println(studentDto);
}Code language: Java (java)

We are using Java Text Blocks to hold the multiline JSON String. When we print the created instance, we get:

StudentDto(id=1111, firstName=Strong, lastName=Belwas, joinedYear=2022)Code language: Bash (bash)

JSON to Java Maps using Jackson

Similarly, we can deserialise JSON strings into Maps quite easily.

private static void jsonToStringMap() throws JsonProcessingException {
    String json = """
            {
                "one":"a",
                "two":"b"
            }
            """;

    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, String> map = objectMapper.readValue(json, new TypeReference<>() {});

    System.out.println(json);
}Code language: Java (java)

Notice that we are passing an Anonymous subclass of TypeReference. That is because the result we expect is of a generic type.

Output:

{"one":"a","two":"b"}Code language: JSON / JSON with Comments (json)

JSON to Java Map of Custom Objects using Jackson

Also, we can read a JSON string and convert it into a Map of custom objects.

private static void jsonToObjectMap() throws JsonProcessingException {
    String json = """
            {
                "1111":{
                    "id":1111,
                    "firstName":"Strong",
                    "lastName":"Belwas",
                    "joinedYear":2022
                }
            }
            """;

    ObjectMapper objectMapper = new ObjectMapper();
    Map<Long, StudentDto> map = objectMapper.readValue(json, new TypeReference<>() {});

    System.out.println(map);
}Code language: Java (java)

We are mapping the JSON string to Map<Long, StudentDto>. The output we get looks like this:

{1111=StudentDto(id=1111, firstName=Strong, lastName=Belwas, joinedYear=2022)}Code language: JSON / JSON with Comments (json)

Summary

In this practical tutorial, we have learned to use Jackson Databind Library To Convert JSON String into Java Objects. We covered examples of Converting JSON to Custom Java Objects, JSON to Java Maps, and JSON to Java Map of Custom Objects.

For more Java Tutorials, please visit Java Tutorials.