Home » Parsing String to JsonNode Jackson

Parsing String to JsonNode Jackson

by Online Tutorials Library

Parsing String to JsonNode Jackson

In this section, we will understand how we can use Jackson 2.0 for converting a JSON String to a JsonNode. JsonNode is one of the most used classes of Jackson. It is an immutable class; means we cannot actually build an object graph of JsonNode instances. Instead, we can create an object graph of the subclass of JsonNode, i.e., ObjectNode.

The process of parsing JsonString into JsonNode is very simple. We simply create an instance of ObjectMapper class and use its readTree() method in the following way:

Let’s take an example to understand how the readTree() method is used for parsing.

StringToJsonNodeExample.java

Output:

Parsing String to JsonNode Jackson

Many times, we need to go lower level for accessing the data from the JsonString. For this purpose, we use the JsonFactory, JsonParser, JsonNode and getFactory(), createParser() and readTree() methods in the following way:

Let’s take an example to understand how these three classes and their methods are used for parsing.

LowLevelParsingExample.java

Output:

Parsing String to JsonNode Jackson


You may also like