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;


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) {

And here is the definition 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) { = id; = name;

    // Getters & Setters

    public String toString() {
        return "Artist{" +
            "id=" + id +
            ", name='" + name + '\'' +

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

<!-- -->

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳


Leave a Reply

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