How to convert JSON String to Java Object

In this tutorial, we will learn how to convert JSON String to Java Object using the JACKSON library. Using a simple Java program we can easily convert JSON String to Java Object.

JSON is “self-describing” and easy to understand. Nowadays, storing data in JSON String is very common instead of XML. But using JSON String every time is not a good option, we need to convert JSON String to Java Object to retrieve data. To achieve this there are some open-source libraries available with the help of which we can convert JSON String to Java Object
In this tutorial, we are going to use the JACKSON library to convert JSON String to Java Object.

Basically, this library has class ObjectMapper using which we can serialize or deserialize Java Object to JSON String or JSON String to Java object. This class has read/write methods using which we can achieve the above tasks.

Example of JSON:

{
"roll_no":1,
"name":"abc",
"branch":"CS"
}

Also read: How to convert JSON to XML using Java

How to Convert JSON String to Java Object using JACKSON library

Using the Following steps we can convert JSON String to Java Object:

  • Create a maven project.
  • Add the following dependency in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0-rc2</version>
</dependency>
  • Create one Java class as a Student which has some variables say roll_no, name, branch as shown below:
package jsontojava;

public class Student {   
  //create variables
  private int roll_no;
  private String name;
  private String branch;
  //default constructor
  public Student() {
    super();
  }
  //another constructor
  public Student(int roll_no, String name, String branch) {
    this.roll_no=roll_no;
    this.name=name;
    this.branch=branch;
  }
  //getters to return values
  public int getRoll_no() {
    return roll_no;
  }
  public String getName() {
    return name;
  }
  public String getBranch() {
    return branch;
  }
}
  • Now create another Java class.
  • Import JACKSON packages.
  • Create ObjectMapper object as shown below:
ObjectMapper om=new ObjectMapper();
  • Create one String variable to store JSON String.
String json="{\"roll_no\":1,\"name\":\"abc\",\"branch\":\"CS\"}";
  • Create an object of Student class and using the readValue() method read the content as shown below:
Student s=om.readValue(json, Student.class);
  • Finally, print the values.

Following is the code of our task:

package jsontojava;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class json {

  public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    // TODO Auto-generated method stub
    //declare ObjectMapper class
    ObjectMapper om=new ObjectMapper();

    //create JSON String
    String json="{\"roll_no\":1,\"name\":\"abc\",\"branch\":\"CS\"}";

    //create Student class object and read the content
    Student s=om.readValue(json, Student.class);

    //print the values from JSON String
    System.out.println("Roll NO = "+s.getRoll_no());
    System.out.println("Name = "+s.getName());
    System.out.println("Branch = "+s.getBranch());
  }

}

Output:

Roll NO = 1
Name = abc
Branch = CS

This is how we can convert JSON String to Java Object. I hope you find this tutorial useful.

Leave a Reply

Your email address will not be published. Required fields are marked *