Tech and Media Labs
This site uses cookies to improve the user experience.

Jackson JsonNode

Jakob Jenkov
Last update: 2019-02-24

The Jackson JsonNode class, com.fasterxml.jackson.databind.JsonNode is Jackson's tree model (object graph model) for JSON. Jackson can read JSON into a JsonNode instance, and write a JsonNode out to JSON. This Jackson JsonNode tutorial will explain how to deserialize JSON into a JsonNode and serialize a JsonNode to JSON. This Jackson JsonNode tutorial will also explain how to build JsonNode object graphs from scratch, so you can serialize them to JSON later.

JsonNode vs. ObjectNode

The Jackson JsonNode class is immutable. That means, that you cannot actually build an object graph of JsonNode instances directly. Instead, you create an object graph of the JsonNode subclass ObjectNode. Being a subclass of JsonNode you can use the ObjectNode everywhere you can use a JsonNode. You will see how to do build ObjectNode graphs later in this tutorial.

Read JsonNode From JSON

To read JSON into a JsonNode with Jackson, you start by creating a Jackson ObjectMapper instance. On the ObjectMapper instance you call readTree() passing the JSON source as parameter. Here is an example of deserializing JSON into a JsonNode:

String json = "{ \"f1\" : \"v1\" } ";

ObjectMapper objectMapper = new ObjectMapper();

JsonNode jsonNode = objectMapper.readTree(json);


Write JsonNode to JSON

To write a JsonNode into JSON with Jackson, you also need a Jackson ObjectMapper instance. On the ObjectMapper you call the writeValueAsString() method, or whatever write method that fits your needs. Here is an example of writing a JsonNode to JSON:

ObjectMapper objectMapper = new ObjectMapper();

JsonNode jsonNode = readJsonIntoJsonNode();

String json = objectMapper.writeValueAsString(jsonNode);

The readJsonIntoJsonNode() method is just a method I have created which parses a JSON string into a JsonNode - just so we have a JsonNode to write. The exact contents of readJsonIntoJsonNode() is not really important for this example, as long as you know it produces a JsonNode object. What matters is the following line that calls the ObjectWriter writeValueAsString() method, which writes the JsonNode into a JSON string.

The Jackson ObjectMapper has many more write methods you can use than writeValueAsString(). You can see these methods in the Jackson ObjectMapper Tutorial.

Get JsonNode Field

A JsonNode can have fields just like a JSON object has. Imagine you have parsed the following JSON into a JsonNode:

    "field1" : "value1",
    "field2" : 999

This JSON object has two fields named field1 and field2. If you had a Jackson JsonNode representing the above JSON object, you could obtain the two fields like this:

JsonNode jsonNode = ... //parse above JSON into a JsonNode

JsonNode field1 = jsonNode.get("field1");
JsonNode field2 = jsonNode.get("field2");

Notice, that even if the two fields are String fields, the get() method always returns a JsonNode to represent the field.

Convert JsonNode Field

The Jackson JsonNode class contains a set of methods that can convert a field value to another data type. For instance, convert a String field value to a long, or the other way around. Here is an example of converting a JsonNode field to some of the more common data types:

String f2Str = jsonNode.get("f2").asText();
double f2Dbl = jsonNode.get("f2").asDouble();
int    f2Int = jsonNode.get("f2").asInt();
long   f2Lng = jsonNode.get("f2").asLong();

Imagine the f2 field contains the value 123456, then it is possible to convert that value to a String, double, int and long as shown above.

Create an ObjectNode

As mentioned earlier, the JsonNode class immutable. To create a JsonNode object graph you must be able to mutate the JsonNode instances in the graph, e.g. setting property values and child JsonNode instances etc. Being immutable, you cannot do that directly with a JsonNode.

Instead you create an ObjectNode instance which is a subclass of JsonNode. Here is an example of creating an ObjectNode via the Jackson ObjectMapper createObjectNode() method:

ObjectMapper objectMapper = new ObjectMapper();

ObjectNode objectNode = objectMapper.createObjectNode();

Set ObjectNode Field

To set a field on a Jackson ObjectNode you can call its set() method, passing a field name String and JsonNode as parameters. Here is an example of setting a field on an Jackson ObjectNode :

ObjectMapper objectMapper = new ObjectMapper();
ObjectNode parentNode = objectMapper.createObjectNode();

JsonNode childNode = readJsonIntoJsonNode();

parentNode.set("child1", childNode);

Again, just imagine that the readJsonIntoJsonNode() produces some JsonNode object which we would like to set as child on the ObjectNode parent object.

Put ObjectNode Field With Primitive Value

The ObjectNode class also has a set of methods that enables you to put (set) primitive values for fields. This is easier than trying to convert a primitive value to a JsonNode and set it with set(). Here is an example of setting a string value for a field on an ObjectNode with the put() method:

objectNode.put("field1", "value1");
objectNode.put("field2", 123);
objectNode.put("field3", 999.999);

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC