Nasza strona używa cookies. Korzystając ze strony, wyrażasz zgodę na używanie cookies, zgodnie z aktualnymi ustawieniami przeglądarki. Rozumiem

Java JSON Data Binding

Bartosz Walczak Software Developer
Learn how to tackle data binding in Java while using Jackson — JSON processor for Java that handles conversion.
Java JSON Data Binding

Nowadays the common practice is to operate on JSON format of files between services. Operating directly on JSON can not always be done easily and in some cases, you may not even have a proper mechanism supporting conversion. Fortunately, there are plenty of open-source tools allowing you to do so. The article below consists of a handful of instructions for working effectively with Jackson in order to handle data binding alongside information about data binding as a whole.


What is Data Binding?

Data Binding is usually thought of as a technique that binds data sources from the provider and consumer together. In frontend side of the software approach, the term data binding is used in cases where a representation of data in an element changes, and the underlying data is updated automatically to reflect this change. Data Binding in the context of Java dives more into the topics of conversion one data source into Java object or the exact other way starting with Java object that needs to be mapped to the external file of the format like JSON. 


Data Binding in Java

When it comes to Java and the most widely used data format, data binding topic usually comes down to two types of processes:

  1. Converting JSON data to a Java POJO
  2. Converting Java POJO to JSON


These two processes differ in terms of used methods to translate one instance into another. You would usually try to read JSON string or file in order to use its intents to populate Java object. You can also go the other way round and start with Java POJO and then send it down to the JSON file. Either of these operations is called Data Binding, but you can also come across other fancy meanings like Mapping, Serialization/Deserialization or Marshalling/Unmarshalling.


Data Binding with Jackson

The whole process of binding data using Java can be really simple if only you know the right tools. Thanks to Jackson, which is an efficient JSON processor for Java that handles conversion without you doing the heavy lifting. Jackson has a strong correlation with Spring Framework, but it actually is a separate project for doing data binding. Jackson has built-in support for formats like JSON, XML, and more.


JSON to Java POJO

The first and the most common use case of data binding is the situation when you would like to convert JSON to Java object. We have a JSON object on the left with fields like: id, age, lastName, and department and we are going to map it to an object that can be used for later development. Employee class on the right consists of the same four fields as our JSON, so id, age, lastName, and department. Our Employee class also lists setter methods for every field as it is necessary for performing conversion.

When it comes to methods in our Employee class we only have to make sure we put there setter methods for given fields. During the mapping process Jackson will call all of the mentioned methods itself behind the scenes. However, it does not access any internal private fields directly, only setter methods. 

Before proceeding let’s take care of the dependencies needed. There are only two which we are going to use - Lombok and Jackson. 

Jackson will be used for obvious reasons, but Lombok will make Employee class look clean, it will take care of getters and setters providing us with @Data annotation. So finally Employee will have the appearance of something like this:

To perform data binding we are not only going to need Employee class and our JSON file, but also Driver class. 

Our Driver class is simple, with only the main method. What we will need for this class is for it to import java.io.File and Jackson object mapper to help us map JSON file to a Java object. Then we need to create an object mapper, which is something that enables us to perform the conversion and it actually comes from Jackson Object Mapper. Next, we call the readValue method on our mapper. We provide the pathname of our JSON file as the first parameter and class of which Jackson will create instance of as well as populate it with contents of our JSON. The last thing left is to output the result to the console. 

And our output looks like this:


Java POJO to JSON

While converting a Java object to JSON what we actually want to do is to send data based on our Employee object over to the JSON output file. During this, Jackson will call appropriate getter methods on our object to collect and send relevant information.

At this time our Employee object has four fields set as following: id: 7, age: 24, lastName: Wilson, department: HR, and that should also be our output. In order to successfully map object and achieve desired result in an external output JSON file, we need to slightly change Driver class. 

We need to call two methods on mapper - enable and writeValue. The former method basically gives us a pretty-printing of the JSON, so it looks good in the actual output file, and in the latter, we provide the destination of our result as well as our actual object that we want to write out. 

Result in output.json: 

We did not modify anything so the result is the same as the contents of our previous sample.json file, here Jackson created a new file with data of our Java object for us.


Wrap up

Data binding using Java can be done amazingly easy. With the convenient use of Jackson we really do not have anything to worry about. The amount of code produced closes in just a few lines and the whole process of converting data from one source to another can be done in the blink of an eye.

1 komentarz

Lubisz dzielić się wiedzą i chcesz zostać autorem?

Podziel się wiedzą z 160 tysiącami naszych czytelników

Dowiedz się więcej