jsondecode.com logo

HomeChevronBlogChevronJSON to Java: Object Mapping with Jackson and Gson

Blog post

JSON to Java: Object Mapping with Jackson and Gson

Map JSON to Java objects using Jackson and Gson — annotations, generics, custom serializers, and Spring Boot integration.

author

Shashank Jain

Author

14/06/20261 minute 22 seconds read
JSON to Java: Object Mapping with Jackson and GsonJSON to Java: Object Mapping with Jackson and Gson

Article

Jackson: The Standard Choice

Jackson is the most widely used JSON library for Java and is bundled with Spring Boot:

// Maven dependency
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.17.0</version>
</dependency>
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        // Deserialize JSON to Java object
        String json = "{\"name\":\"Alice\",\"age\":30}";
        User user = mapper.readValue(json, User.class);
        System.out.println(user.getName()); // Alice

        // Serialize Java object to JSON
        String output = mapper.writeValueAsString(user);
        // or pretty print:
        String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
    }
}

Jackson Annotations

import com.fasterxml.jackson.annotation.*;

public class User {
    @JsonProperty("user_id")
    private int id;

    private String name;

    @JsonIgnore
    private String password;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String email;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private LocalDate createdAt;

    // getters and setters...
}

Handling Generic Types

// List of objects
List<User> users = mapper.readValue(json, new TypeReference<List<User>>() {});

// Map
Map<String, User> userMap = mapper.readValue(json, new TypeReference<Map<String, User>>() {});

Gson: Google's Alternative

// Gradle
implementation 'com.google.code.gson:gson:2.10.1'
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Gson gson = new GsonBuilder().setPrettyPrinting().create();

// Deserialize
User user = gson.fromJson(json, User.class);

// Serialize
String json = gson.toJson(user);

// Generic types
Type listType = new TypeToken<List<User>>(){}.getType();
List<User> users = gson.fromJson(json, listType);

Jackson vs Gson

FeatureJacksonGson
PerformanceFasterSlower
Spring BootBuilt-inNeeds config
AnnotationsRichBasic
Null handlingConfigurableIncludes nulls by default
Learning curveSteeperSimpler

FAQ

Which should I use — Jackson or Gson?

Use Jackson for Spring Boot projects (it is already included) and for production systems where performance matters. Use Gson for simpler projects or when you prefer its cleaner API.

How does Jackson handle missing JSON fields?

By default, Jackson ignores missing JSON fields (leaves Java fields at their default values). Configure DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to throw on unknown fields.

How do I parse JSON arrays in Java?

With Jackson: mapper.readValue(json, new TypeReference<List<MyClass>>() {}). With Gson: gson.fromJson(json, new TypeToken<List<MyClass>>(){}.getType()).

How do I convert JSON to a Java Map?

Jackson: mapper.readValue(json, Map.class) for Map<String, Object>, or new TypeReference<Map<String, YourType>>() {} for typed values.

How do I handle dates in Jackson?

Register the JavaTimeModule: mapper.registerModule(new JavaTimeModule()). Then annotate date fields with @JsonFormat to control the serialization pattern.

Keep reading

Recent blogs

View all

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.