Search

Suggested keywords:
  • Java
  • Docker
  • Git
  • React
  • NextJs
  • Spring boot
  • Laravel

Understanding Basics of Jackson JSON Parser

  • Share this:

post-title

Jackson is a popular open-source Java library for working with JSON data. It provides powerful capabilities to serialize Java objects into JSON and deserialize JSON back into Java objects. In this article, we'll explore the basics of using Jackson for JSON processing in Java applications.

 

Adding Jackson to Your Project

To use Jackson in your Java project, you need to add the appropriate dependencies. If you are using Apache Maven, include the following dependency in your pom.xml file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
</dependency>

 

If you are using Gradle, add this to your build.gradle file:

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
}

 

Let's have a Person class representing a person with name and age:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Person {
    private String name;
    private int age;

    // Getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

Serialization: Converting Java Objects to JSON

Create an object of Person and set values to the fields. Serialize it using ObjectMapper function.

 public static void serialize() {
        // Create ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();

        // Create a Person object
        Person person = new Person();
        person.setName("Muthu Annamalai");
        person.setAge(22);

        // Convert Person object to JSON
        String json = objectMapper.writeValueAsString(person);
        System.out.println("JSON: " + json);
    }

The above function will serialize the object as JSON string

{ "name":"Muthu Annamalai","age":22 }.

 

Deserialization: Converting JSON to Java Objects

Deserialization is a reverse process of serialization. In this we will convert JSON string back to object.

public static void deserialize() {
        // Create ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();

        // JSON to Java
        String json = "{\"name\":\"Jane Smith\",\"age\":28}";
        Person person = objectMapper.readValue(json, Person.class);

        // Print Java object
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }

 

Customizing Serialization and Deserialization

Jackson provides annotations to customize the serialization and deserialization process. For example, you can use @JsonProperty to specify the JSON property name, @JsonFormat to define the format for dates, and more.


Suppose we want to change the JSON property name for the name field:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
    @JsonProperty("full_name")
    private String name;
    private int age;

    // Getters and setters
    // ...
}

With this change, when we serialize a Person object, the JSON will have "full_name" instead of "name".

{ "full_name":"Muthu Annamalai","age":22 }.

 

Conclusion

Jackson is a powerful Java library for JSON processing, providing easy-to-use APIs for both serialization and deserialization of Java objects.  We covered how to add Jackson to your project, serialize Java objects to JSON, and deserialize JSON into Java objects in this article.

By leveraging Jackson, Java developers can easily work with JSON data in their applications, making it an essential tool for handling RESTful web services, working with APIs, and data interchange. Explore the official Jackson documentation for more advanced features and examples.

Muthu Annamalai

About author
Technical Writer | Pre-Final Year Student | Code & Community | Developer who works everyday to improve himself.