Home » Tree Model Nodes in Jackson

Tree Model Nodes in Jackson

by Online Tutorials Library

Tree Model Nodes in Jackson

In Jackson, Tree Model Node is one of the most important concepts which we are going to discuss in this section. We will use Tree Model Node for various conversions and for adding, modifying, and removing nodes.

Let’s understand how we can create a Node, transform a Node, manipulate a Node, locate a Node, and iterate over the Nodes one by one:

Tree Model Nodes in Jackson

Creating a Node

First, we will create an instance of the ObjectMapper class with the help of the default constructor.

ObjectMapper mapper =new ObjectMapper();

Now, we can construct a JsonNode from any one of the following three ways:

  1. We can create a Node From Scratch, i.e., the simplest way of creating a Node. However, we also have two ways to create a JsonNode from scratch also:
    1. By using the createObjectNode() method of the ObjectMapper class.
      JsonNode node = mapper.createObjectNode();
    2. We can also create a Node from the JsonNodeFactory class in the following way:
      JsonNode node = JsonNodeFactory.instance.objectNode();
  2. We can also create a JsonNode by parsing from a JSON source. To learn more about this way, go through the following link:
    https://tutoraspire.com/parsing-string-to-jsonnode-jackson
  3. We can also create JsonNode by converting it from the Java object. We use the valueToTree() method of the ObjectMapper class to form JsonNode from a Java object.
    JsonNode node = mapper.valueToTree(fromValue)
  4. Alternatively, we can also use the convertValue API for this purpose.
    JsonNode node = mapper.convertValue(fromValue, JsonNode.class)

Let’s take an example to understand how we can create a JsonNode by using the valueToTree() method.

ValueToTreeJsonNode.java

Output:

Tree Model Nodes in Jackson

Transforming a Node

We can transform a Tree Node into a string or Java object.

Tree Model Nodes in Jackson

Convert Tree Node into a JSON String

We transform a Tree Node into a JSON string when we need to write a Tree Node as JSON to the destination. Here, the destination can be an OutputStream, a File, or a writer. We use the writeValue() method of the mapper class in the following way:

mapper.writeValue(destination, node)

Let’s take an example to understand how we can use the writeValue() method to write JsonNode as JSON into the destination file.

WriteOutAsJSONExample.java

Output:

Tree Model Nodes in Jackson

Convert Tree Node into a Java object

We can also transform JSON Node into a Java Object by using the treeToValue API. The treeToValue() method of the ObjectMapper class accept two parameters, i.e., JsonNode and Object.class:

The treeToValue() method is similar to the convertValue() method of ObjectMapper class.

We can also convert JsonNode to Java Object by using the token stream. We use the treeAsTokens() and readValue() methods in the following way:

Let’s take an example in which we will convert a JsonNode into a Java object by using all these three ways:

ConvertJsonNodeToJavaObject.java

Output:

Tree Model Nodes in Jackson

Manipulating Tree Nodes

We can manipulate Tree Nodes by using Jackson. Manipulating Tree Nodes include Locating a Node, Adding a New Node, Editing a Node, and Removing a Node.

Let’s understand each one of them one by one with an example:

Locating a specific Node

We use the following steps to locate a specific JsonNode from JSON:

  1. Form JSON string(can read JSON from a file as a string or can serialize a Java object as a string).
  2. Use the readTree() method of the ObjectMapper class to get the root node of the JSON tree.
  3. Use the path() method of the JsonNode to reach out to the specific JsonNode.
  4. Print the value of the JsonNode by using the textValue() method of JsonNode.

Let’s implement the code for locating a specific node by using above discussed steps:

ManipulatingNodeExample1.java

Output:

Tree Model Nodes in Jackson

Adding a New Node

We can add a new node as a child node of another node. We use the put() method for adding a node. We use put() method in the following way:

Jackson provides several overloaded variants of the put method, which we can use for adding new nodes of different value types.

However, Jackson provide several other methods for adding child node such as putArray(), putObject(), putPOJO(), putRawValue() and putNull().

Let’s take an example to understand how we can use put() method to add a node as a child.

ManipulatingNodeExample2.java

Output:

Tree Model Nodes in Jackson

Editing a Node

Just like adding a new JsonNode, we can edit the value of a specific JsonNode. We use the following steps for editing a specific JsonNode:

  1. Form JSON string(can read JSON from a file as a string or can serialize a Java object as a string).
  2. Use the readTree() method of the ObjectMapper class to get the root node of the JSON tree.
  3. Convert JsonNode into ObjectNode in the following way:
    ObjectNode obj = ((ObjectNode) rootNode)
  4. We use the set() method for overriding a specific JsonNode.

Let’s implement the code for editing a specific node by using above discussed steps:

ManipulatingNodeExample3.java

Output:

Tree Model Nodes in Jackson

Removing a Node

Just like adding or editing a node, we can also remove a specific node from the JsonNode. We remove a specific node by calling the remove() method or remove API on its parent node. We use the following steps for editing a specific JsonNode:

  1. Form JSON string(can read JSON from a file as a string or can serialize a Java object as a string).
  2. Use the readTree() method of the ObjectMapper class to get the root node of the JSON tree.
  3. Convert JsonNode into ObjectNode in the following way:
    ObjectNode obj = ((ObjectNode) rootNode)
  4. We use the remove() method for removing a specific JsonNode.

Let’s implement the code for removing a specific node by using above discussed steps:

ManipulatingNodeExample4.java

Output:

Tree Model Nodes in Jackson

Iterating over the Nodes

In order to iterate over the nodes, we first need to reformat them into YAML. A JSON document can contain three types of nodes, i.e., Value, Object, and Array. JSON document contains nodes in a hierarchical tree structure. So, we will start iterating the JSON document at the top and go through all the child nodes.

We will use a recursive process for iterating the JSON nodes. In the recursive method, we will pass the root node of the JSON document.

We will use the following steps for iterating over the nodes:

  1. Form JSON string(can read JSON from a file as a string or can serialize a Java object as a string).
  2. Use the readTree() method of the ObjectMapper class to get the root node of the JSON tree.
  3. Create a recursive method, i.e., processNode(), in which we pass the root node, yaml, and index.
  4. The processNode() method handles different node types. It uses JsonNode.fields because it provides us access to both the field name and field value.
  5. The processNode() method adds the field name to the output and then processes its value as a child node by passing it to the processNode() method.

Let’s implement the code for iterating all the nodes by using the above-discussed steps:

ManipulatingNodeExample5.java

Output:

Tree Model Nodes in Jackson


You may also like