Java JSON tutorial

This is a Java JSON tutorial. Here, you will learn what JSON is and how to work with JSON format in Java. You will see where to find the required JSON libraries and parse Java Objects to JSON and vice versa.

Let’s get started.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text data format widely used, and that is mainly because of its readability. It’s easy for machines to parse and generate.

We use it to transmit data in web applications (e.g., sending some data from the server via HTTPs/HTTP).
It consists of key-value pairs (representing a Java object) or an ordered list of values (arrays, lists…).

JSON example

{
   "student":{  // object
      "id":"1209",
      "grade":"9.2",
      "lectures":{ // object
         "data_science_for_business":[ // an array of objects
            {
               "name":"Leverage data science",
               "duration":"2"
            },
            {
               "name":"Identify data and missing components",
               "duration":"1.5"
            },
            {
               "name":"Understand key techniques",
               "duration":"3.5"
            }
         ]
      }
   }
}


Let’s see what we have here. We have a “student” object which consists of a couple of key-value pairs and a “lectures” object. The “lectures” object contains one array, “data_science_for_business”, that contains objects with fields “name” and “duration”.

As you can see, JSON starts with an open curly bracket ‘{‘ and ends with a closed one ‘}’.  To find out more about JSON, visit json.org.

Working with JSON in Java

In Java, we often work with the JSON format. We convert Java Objects to JSON, write and read JSON files, send HTTPs/HTTP request bodies in JSON format, and much more.

To work with JSON in Java, we need to have the appropriate libraries imported into the project. Many libraries deal with JSON. The most popular are Jackson and gson. In this tutorial, we will use the Jackson library.

How to use Jackson library in Java?

If you are using Maven, then all you need to do is to add the following dependency into your pom.xml:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.4</version>
</dependency>

The above dependency will automatically download the following jars:

  • Jackson Databind
  • Jackson Core
  • Jackson Annotations

If you are not using a Maven project, you will need to download these jars and manually import them into your project.  

ObjectMapper class

With Jackson library, we get the ObjectMapper class that provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model.

It contains some useful methods that we can use to process the JSON values.  We will use ObjectMapper a lot in the upcoming lessons because it is the main class for JSON processing.

JsonNode class

JsonNode class is the base class for all JSON nodes, which form the JSON Tree Model that Jackson implements. When we want to convert a Java Object into JSON, we will convert it into a JsonNode using the methods from the ObjectMapper.

Now, when you got the introduction and the required jars, it is time to start with the lessons.

Happy Learning and Have Fun!