Master Dynamic JSON Fields in Java with @JsonAnyGetter and @JsonAnySetter
This article explains how to handle JSON objects with unknown or dynamic fields in Java using Jackson's @JsonAnySetter to collect extra properties into a Map and @JsonAnyGetter to serialize them back as regular fields, complete with practical code examples and common pitfalls.
Handling Dynamic JSON Properties in Java
When a JSON response contains fields that are not known at compile time, Jackson provides two annotations— @JsonAnySetter and @JsonAnyGetter —to manage these dynamic attributes without breaking the normal deserialization and serialization process.
@JsonAnySetter: Collecting Unknown Fields
The @JsonAnySetter annotation marks a method that receives any JSON property that does not have a matching Java field. Typically the method stores the key‑value pair in a Map<String, Object> called additionalProperties (or any name you prefer).
public void addAdditionalProperty(String key, Object value) {
additionalProperties.put(key, value);
}Example JSON:
{
"name": "豆瓣酱",
"spicy": true,
"limited_edition": "yes",
"extra_notes": "只在冬天卖"
}If the Java class defines only name and spicy, the fields limited_edition and extra_notes are automatically added to the map during deserialization.
Complete Deserialization Example
public class Person {
private String name;
private int age;
private Map<String, Object> additionalProperties = new HashMap<>();
@JsonAnySetter
public void addAdditionalProperty(String key, Object value) {
additionalProperties.put(key, value);
}
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}
}
ObjectMapper mapper = new ObjectMapper();
Person p = mapper.readValue(jsonString, Person.class);
System.out.println(p.getAdditionalProperties()); // {address=123 Street, nickname=Johnny}@JsonAnyGetter: Serializing Dynamic Fields
The @JsonAnyGetter annotation marks a method that returns the map of dynamic properties. During serialization, Jackson flattens the map entries into normal JSON fields.
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return additionalProperties;
}Using the same Person object, the following code produces JSON that includes both static and dynamic fields:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(person);
System.out.println(json); // {"name":"John","age":30,"address":"123 Street","nickname":"Johnny"}Key Takeaways
Define a Map<String, Object> in the class to store extra properties.
Annotate a setter method with @JsonAnySetter to capture unknown fields during deserialization.
Annotate a getter method with @JsonAnyGetter to emit those fields during serialization.
This approach is ideal for JSON structures whose schema may change, such as configuration objects, plugin metadata, or loosely defined APIs.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
