How to modify JsonNode in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
JSON, or JavaScript Object Notation, is a popular data interchange format due to its simplicity and readability. In Java, the JsonNode class from the Jackson library is widely used to work with JSON data. This article provides a deep dive into how to modify a JsonNode in Java using the Jackson library, complete with examples and technical explanations.
Understanding JsonNode
JsonNode is an immutable object representing a JSON structure. It is part of the Jackson library, often used to parse JSON data into a tree-like structure. Given its immutable nature, direct modifications to a JsonNode instance are not possible. To "modify" a JsonNode, you typically work with a mutable representation of the JSON structure, such as ObjectNode or ArrayNode.
Working with ObjectNode and ArrayNode
These classes extend JsonNode and provide mutable operations to effectively modify JSON content:
- ObjectNode: Represents a JSON object (key-value pairs).
- ArrayNode: Represents a JSON array.
Both of these classes allow for dynamic modification of JSON data, such as adding, updating, or removing fields and values.
Parsing JSON to JsonNode
Before modifying a JsonNode, you need to parse your JSON data:
In the snippet above, readTree parses the JSON string into a JsonNode.
Modifying JsonNode Using ObjectNode
To modify data within a JsonNode, you can cast it to ObjectNode if it represents a JSON object:
Key Operations on ObjectNode
- Add/Update a Field:
put(String fieldName, String value)adds or updates a field in the JSON object. - Remove a Field:
remove(String fieldName)removes a field from the JSON object. - Check Existence:
has(String fieldName)checks the presence of a field.
Modifying JsonNode Using ArrayNode
For JSON arrays, you can cast JsonNode to ArrayNode:
Key Operations on ArrayNode
- Add Element:
add(String value)appends a new value to the array. - Update Element:
set(int index, String value)updates the value at a specific position. - Remove Element:
remove(int index)removes an element from the array.
Example Summary
The table below summarizes the key operations and their respective functionalities for ObjectNode and ArrayNode.
| Operation | ObjectNode Method | ArrayNode Method | Description |
| Add/Update | put(String, String) | add(String) / set(int, String) | Add or modify field/element |
| Remove | remove(String) | remove(int) | Remove field/element |
| Check Existence | has(String) | N/A | Check field existence (only for ObjectNode) |
Converting Back to JSON String
After modification, you can convert JsonNode back to a JSON string using ObjectMapper's writeValueAsString method:
The above code converts a JsonNode back to a JSON-formatted string, preserving modifications.
Conclusion
Manipulating JSON data in Java involves understanding JsonNode and its subclasses ObjectNode and ArrayNode. These classes provide functionality to modify JSON content dynamically, enabling developers to seamlessly integrate JSON processing into their applications. With the Jackson library's capabilities, handling JSON transformations becomes efficient and robust.

