Java
JsonNode
JSON manipulation
Jackson library
Java programming

How to modify a JsonNode in Java (Jackson ObjectNode and ArrayNode)

Codemia Knowledge Hub ·

You cannot change a Jackson JsonNode through the JsonNode type itself, because it is treated as immutable. To modify it, check what kind of node it is and cast it to the mutable subclass: ObjectNode for a JSON object (use put, remove) or ArrayNode for a JSON array (use add, set, remove). Then serialize it back with ObjectMapper.writeValueAsString.

  • ObjectNode represents a JSON object (key-value pairs).
  • ArrayNode represents a JSON array.

Both extend JsonNode and let you add, update and remove fields and values.

Parse the JSON into a JsonNode

java
1import com.fasterxml.jackson.databind.JsonNode;
2import com.fasterxml.jackson.databind.ObjectMapper;
3
4public class JsonNodeExample {
5    public static void main(String[] args) throws Exception {
6        String jsonString = "{\"name\": \"John\", \"age\": 30}";
7        ObjectMapper objectMapper = new ObjectMapper();
8        JsonNode rootNode = objectMapper.readTree(jsonString);
9        System.out.println("Original JsonNode: " + rootNode);
10    }
11}

readTree parses the string into a JsonNode tree.

Set or update a field with ObjectNode

If the node is a JSON object, cast it to ObjectNode:

java
1import com.fasterxml.jackson.databind.JsonNode;
2import com.fasterxml.jackson.databind.ObjectMapper;
3import com.fasterxml.jackson.databind.node.ObjectNode;
4
5public class JsonNodeModification {
6    public static void main(String[] args) throws Exception {
7        String jsonString = "{\"name\": \"John\", \"age\": 30}";
8        ObjectMapper objectMapper = new ObjectMapper();
9        JsonNode rootNode = objectMapper.readTree(jsonString);
10        
11        if (rootNode.isObject()) {
12            ObjectNode objectNode = (ObjectNode) rootNode;
13            // Add or update properties
14            objectNode.put("name", "Jane");
15            objectNode.put("city", "New York");
16        }
17        
18        System.out.println("Modified JsonNode: " + rootNode);
19    }
20}

Useful ObjectNode methods:

  • put(String fieldName, String value) adds or updates a field.
  • remove(String fieldName) removes a field.
  • has(String fieldName) checks whether a field exists.

Add or replace array elements with ArrayNode

For a JSON array, cast to ArrayNode:

java
1import com.fasterxml.jackson.databind.JsonNode;
2import com.fasterxml.jackson.databind.ObjectMapper;
3import com.fasterxml.jackson.databind.node.ArrayNode;
4
5public class ArrayNodeModification {
6    public static void main(String[] args) throws Exception {
7        String jsonString = "[\"apple\", \"banana\"]";
8        ObjectMapper objectMapper = new ObjectMapper();
9        JsonNode rootNode = objectMapper.readTree(jsonString);
10        
11        if (rootNode.isArray()) {
12            ArrayNode arrayNode = (ArrayNode) rootNode;
13            // Add elements
14            arrayNode.add("cherry");
15            // Update elements
16            arrayNode.set(0, "orange");
17        }
18        
19        System.out.println("Modified ArrayNode: " + rootNode);
20    }
21}

Useful ArrayNode methods:

  • add(String value) appends a value.
  • set(int index, String value) replaces the value at a position.
  • remove(int index) removes an element.

ObjectNode and ArrayNode methods side by side

OperationObjectNode MethodArrayNode MethodDescription
Add/Updateput(String, String)add(String) / set(int, String)Add or modify field/element
Removeremove(String)remove(int)Remove field/element
Check Existencehas(String)N/ACheck field existence (only for ObjectNode)

Write the modified node back to a string

java
String modifiedJsonString = objectMapper.writeValueAsString(rootNode);
System.out.println("Modified JSON string: " + modifiedJsonString);

The output includes every change made through the ObjectNode or ArrayNode casts, because they modify the same tree.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms