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.
Shashank Jain
Author


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
| Feature | Jackson | Gson |
|---|---|---|
| Performance | Faster | Slower |
| Spring Boot | Built-in | Needs config |
| Annotations | Rich | Basic |
| Null handling | Configurable | Includes nulls by default |
| Learning curve | Steeper | Simpler |
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

Jun 14, 2026
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Serialize and deserialize JSON in C# using System.Text.Json and Newtonsoft.Json with practical examples.

Jun 14, 2026
JSON to Markdown Table: Convert JSON Arrays Instantly
Convert JSON arrays to Markdown tables in JavaScript, Python, and with the free online tool.

Jun 14, 2026
JSON in TypeScript: Type-Safe Parsing and Validation
Stop using any for JSON in TypeScript — use Zod, type guards, and generics for fully type-safe parsing.