Reading and Writing JSON in Java With Jackson

Java doesn’t have native JSON support in its standard library, so you need an external library for it. Jackson is the most widely used one in the Java ecosystem (it’s also Spring Boot’s default library for JSON serialization), so this post covers the basics: reading JSON into an object, writing an object into JSON, and the most common adjustments you’ll need to make.

Adding the dependency

If you’re using Maven, add this to your pom.xml:

<!-- check Maven Central for the latest jackson-databind version -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.17.0</version>
</dependency>

If you’re using Gradle:

// check Maven Central for the latest jackson-databind version
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'

Converting JSON into an object (deserialization)

First, create a Java class that matches the JSON structure.

// java
public class Person {
    private String name;
    private int age;

    // getters and setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}

Then use ObjectMapper to convert the JSON.

// java
import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();

String json = "{\"name\": \"Ana\", \"age\": 30}";
Person person = mapper.readValue(json, Person.class);

System.out.println(person.getName()); // Ana

Jackson uses reflection to match JSON keys with the class’s getters/setters. By default, it expects the JSON key name to match the field name (or its corresponding getter/setter).

Converting an object into JSON (serialization)

// java
Person person = new Person();
person.setName("Ana");
person.setAge(30);

String json = mapper.writeValueAsString(person);
System.out.println(json); // {"name":"Ana","age":30}

Formatting the output JSON (pretty print)

// java
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);

String json = mapper.writeValueAsString(person);

This adds readable indentation to the generated JSON, useful for debugging or logs, though it’s usually unnecessary (and even wastes bandwidth) in production API responses.

Handling fields that don’t match exactly

A common issue: the JSON uses snake_case (common in APIs from other languages) but Java conventions use camelCase. In that case, use the @JsonProperty annotation.

// java
public class Person {
    @JsonProperty("full_name")
    private String fullName;

    // getters and setters
}

This makes Jackson map the JSON key full_name to the Java field fullName, even though the names don’t match.

Ignoring unknown fields

By default, if the JSON has a key that doesn’t exist in the Java class, Jackson throws an exception. To avoid that (for example, when you only need part of the data from a large API response):

// java
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Working with lists and nested structures

To deserialize a JSON array into a Java list, use TypeReference.

// java
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;

String json = "[{\"name\": \"Ana\"}, {\"name\": \"John\"}]";

List<Person> people = mapper.readValue(json, new TypeReference<List<Person>>() {});

Handling parse errors

// java
try {
    Person person = mapper.readValue(invalidJson, Person.class);
} catch (JsonProcessingException e) {
    System.out.println("Error processing JSON: " + e.getMessage());
}

JsonProcessingException is the base exception that covers both JSON syntax errors and mapping problems between the JSON and the Java class.

Quick checklist

  • Does your Java class structure match the JSON structure (or did you use @JsonProperty to map differences)?
  • Did you set FAIL_ON_UNKNOWN_PROPERTIES to false if you expect extra fields you don’t use?
  • Using TypeReference to deserialize lists, instead of trying List.class directly (which doesn’t work because of Java’s type erasure)?
  • Handling JsonProcessingException in code that receives JSON from external sources?

To quickly validate a JSON before mapping it to a Java class, paste it into JSON Validator.