Deserialize JSON string into Java object using ObjectMapper.readValue() with Jackson

Tags: JSON jackson-databind Jackson

Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper.readValue()

This method to deserialize JSON string into a Java object.

Adding Jackson dependency into your project

Using Gradle

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'

Using Maven

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

How to use com.fasterxml.jackson.databind.ObjectMapper.readValue()

package simplesolution.dev;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ObjectMapperReadValueExamples {

    public static void main(String... args) {
        String data = "[{\"name\":\"Sample JSON Serialization\",\"url\":\"https://simplesolution.dev\"},{\"name\":\"Java Tutorials\",\"url\":\"https://simplesolution.dev/java\"}]";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Map<String, String>> objectFromJsonString = objectMapper.readValue(data, List.class);
            System.out.println("Object from String: " + objectFromJsonString);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Happy Coding 😊