We've all been there.

You're developing a Spring Boot application and testing it out. The app receives a JSON object from an upstream service.

It needs to parse that object using Jackson but lo and behold there's no corresponding Java class for that JSON structure.

What do you do?

Well, for many situations, the best solution would be to create a Java class that maps to the structure.

But, for whatever reason, that's not an option here. You just need to take that JSON string and convert into a Java object quickly.

I'll show you how to handle that problem right here.

Meet JsonNode

The JsonNode class is probably what you're looking for. It can handle just about any type of JSON object you need to parse.

Off the top of my head, I can't think of anything it can't handle.

That JsonNode class, by the way, is Jackson's base class for all logical representations of JSON nodes. You can think of it like a DOM node in an XML document.

Bottom line: if you have a JSON object that doesn't map to a class in your Spring Boot application, you can map it to a JsonNode object.

How to Do That

Let's take look at a simple deserialization effort with minified version of an employee object that looks like this:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "shirtSize" : "XXL"
}

Now let's say you don't have a corresponding Employee class to go along with that but you still need to represent it in Java.

In that case, deserialize it this way:

ObjectMapper mapper = new ObjectMapper();  
            
String json = "{\r\n"
        + "  \"id\" : \"A17\",\r\n"
        + "  \"lastName\" : \"Smith\",\r\n"
        + "  \"firstName\" : \"Frank\",\r\n"
        + "  \"shirtSize\" : \"XXL\"\r\n"
        + "}";
            
JsonNode jsonNode = mapper.readTree(json);
System.err.println(jsonNode);

That's going to give you this output:

{"id":"A17","lastName":"Smith","firstName":"Frank","shirtSize":"XXL"}

And that's a success. The JsonNode object properly reflects what was in the JSON object.

But Can You Do Anything With It?

Yes, you can do something with it.

If you'd like to get just one property, for example, you can do that by changing the last line in the deserialization code to this:

System.err.println(jsonNode.get("lastName").asText());

As you can see, that's using the get() method on the JsonNode object.

The string inside that get() method specifies the name of the property to get. In this case, it's "lastName."

But if you stop with get(), you're going to just get another JsonNode object. If you want to actually get the value of that JSON node, you need to extract it according to its data type.

Since the last name is a String, the code above invokes the asText() method on the returned JsonNode object. That returns the last name as a String.

Run that code now and it spits out the following:

Smith

That's the last name so that's correct.

By the way, if you want to convert a JSON node to another type, you've got plenty of options. You can invoke asInt(), asBoolean(), asDouble(), and asLong() as well.  

Just make sure the type in the JSON property matches.

Wrapping It Up

Now you know how to convert a JSON object to a JsonNode object in your Spring Boot application. 

If you're ever in a bind and need to deserialize something with no corresponding class, feel free to use Jackson to do what you learned about today.

Have fun!

Photo by Aline Viana Prado from Pexels