Skip to content

Jackson

The JSON library the JVM ecosystem standardised on.

Web & HTTPWebJava

What it is

Jackson is a high-performance JSON processor for Java. It allows for parsing, generating, and transforming JSON data, and integrates seamlessly with Java objects using annotations and data binding.

Jackson allows Java developers to serialize Java objects to JSON and deserialize JSON into Java objects. It supports annotations for customizing serialization, ignores unknown properties, and integrates with frameworks like Spring Boot.

Licence
Apache 2.0
Watch for
Polymorphic deserialisation has been a source of CVEs — never enable default typing on untrusted input

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Any JSON serialisation on the JVM — it is what Spring Boot uses by default
  • You need streaming for large documents, or support for YAML, XML and CSV through the same API

Look elsewhere when

  • A very small project where Gson's simpler API is enough

Installation

Add com.fasterxml.jackson.core:jackson-databind dependency in pom.xml

Getting started

The smallest useful thing you can do with it, and what each part means.

Serializing a Java object to JSON
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(1, "Alice");
        String json = mapper.writeValueAsString(user);
        System.out.println(json);
    }
}

class User {
    public int id;
    public String name;
    public User(int id, String name) { this.id = id; this.name = name; }
}
Converts a Java object `User` into a JSON string using `ObjectMapper`.
Deserializing JSON to a Java object
String json = "{\"id\":1,\"name\":\"Alice\"}";
User user = mapper.readValue(json, User.class);
System.out.println(user.name);
Converts a JSON string into a Java `User` object using Jackson.

Advanced usage

Where the library earns its place over a simpler alternative.

Ignoring unknown properties
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
class User {...}
Prevents Jackson from failing when extra fields exist in JSON that are not in the Java class.
Custom serialization
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

class CustomUserSerializer extends StdSerializer<User> {
    public void serialize(User user, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("full_name", user.name);
        gen.writeEndObject();
    }
}
Demonstrates customizing how a Java object is serialized to JSON.
Working with Collections
List<User> users = Arrays.asList(new User(1,"Alice"), new User(2,"Bob"));
String json = mapper.writeValueAsString(users);
List<User> deserialized = mapper.readValue(json, new TypeReference<List<User>>() {});
Shows serialization and deserialization of lists of Java objects.
Tree model for dynamic JSON
JsonNode rootNode = mapper.readTree(json);
String name = rootNode.get("name").asText();
Allows parsing JSON into a tree structure for dynamic access without mapping to a Java class.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

JsonMappingException
Occurs when JSON cannot be mapped to Java object. Ensure field names and types match.
JsonParseException
Occurs when JSON is invalid or malformed. Validate JSON input before deserialization.
IOException
Occurs during I/O operations. Handle file/stream errors when reading or writing JSON.

Best practices

  • Use `ObjectMapper` as a singleton to avoid performance overhead.
  • Leverage annotations like `@JsonProperty`, `@JsonIgnore`, and `@JsonInclude` for fine-grained control.
  • Handle unknown properties gracefully with `@JsonIgnoreProperties`.
  • Use TypeReference for deserializing generic types like lists and maps.
  • Integrate with Spring Boot’s `MappingJackson2HttpMessageConverter` for REST APIs.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Jackson was created to provide a flexible, efficient, and feature-rich library for working with JSON in Java. It supports streaming, tree model, and data-binding approaches, making it popular for REST APIs, configuration parsing, and data serialization.