Introduction to JSON Binding in Jakarta EE

JSON Binding is a Jakarta EE specification about converting between JSON data and Java objects, similar to the Jackson library that I introduced to you. In this tutorial, we will learn together about JSON Binding and how to use it with its implementation, Eclipse Yasson.

First, I will create a new Maven project as an example:

Eclipse Yasson dependency is as follows:

As an example, I will define a Student class that contains student information as follows:

I will initialize the object of the Student class first:

To convert this Student object to a JSON string, you need to initialize the object of the Jsonb class from the JsonbBuilder class of the JSON Binding API as follows:

The implementation of these classes in the Yasson library is JsonBinding and JsonBindingBuilder respectively.

After we have the Jsonb object, we can use the toJson() method with the parameter being the object to be converted to a JSON string, to convert it to JSON data:

The entire code is as follows:

The output when running this example application is as follows:

If you have a JSON string for student information and need to convert it to a Student object, you can also use the fromJson() method to do this.

Ví dụ mình có chuỗi JSON chứa thông tin sinh viên như sau:

I can convert to Student object as follows:


You can define an object of the JsonConfig class to add some configuration about the JSON string that will be generated. For example, if you want formatted JSON output, you can write the following code:


Add Comment