How to Dynamically Add, Update, and Remove JSON Fields with Jackson in Java
This tutorial demonstrates how to use Jackson's ObjectMapper and ObjectNode to programmatically add, modify, and delete fields in JSON strings or Java objects, providing clear code examples and explaining the underlying JsonNode API for flexible JSON manipulation.
1. Introduction
In daily development JSON processing is common; we often need to add extra fields or delete specific ones. This article shows how to achieve that using the Jackson library.
2. Adding extra fields to a JSON string
Assume we have the following JSON:
{
"username":"felord.cn",
"age":18
}We want to add a gender field:
{
"username":"felord.cn",
"age":18,
"gender":"male"
}First, use ObjectMapper to read the JSON into an ObjectNode:
ObjectNode jsonNodes = objectMapper.readValue(json, ObjectNode.class);The ObjectNode provides many methods for JSON property manipulation, such as get and the family of put methods. Using put we can add the new field:
String json = "{
" +
" \"username\":\"felord.cn\",
" +
" \"age\":18
" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNodes = objectMapper.readValue(json, ObjectNode.class);
jsonNodes.put("gender", "male");
String newJson = objectMapper.writeValueAsString(jsonNodes);
// newJson = {"username":"felord.cn","age":18,"gender":"male"}3. Adding fields when converting an object to JSON
Sometimes the source is a Java object that does not contain the desired field. By converting the object to an ObjectNode we can apply the same technique.
@Data
public class User {
private String username;
private Integer age;
}Convert the object to an ObjectNode with valueToTree, then add the extra field:
User user = new User();
user.setUsername("felord.cn");
user.setAge(18);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNodes = objectMapper.valueToTree(user);
jsonNodes.put("gender", "male");
String newJson = objectMapper.writeValueAsString(jsonNodes);
// newJson = {"username":"felord.cn","age":18,"gender":"male"}4. Removing properties
Whether working with a JSON string or converting an object, you can remove a property by calling the remove method on the ObjectNode. (Code omitted for brevity.)
5. Extension
All of the above operations rely on Jackson's JsonNode hierarchy, which provides fine‑grained access to JSON data. The diagram below illustrates the relationship:
6. Summary
This article introduced dynamic addition, modification, and deletion of JSON using Jackson's JsonNode API. By leveraging existing Jackson utilities you can solve common JSON manipulation tasks without writing custom parsers or adding extra dependencies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
