Convert JSON String to Java Object Using JSON-Simple Library



In this article, we will learn how to convert a JSON string to a Java object using the json.simple library.

JSON stands for JavaScript Object Notation. It's a simple format that stores data in key-value pairs. It is used for sharing data between a server and a web app. To learn more about JSON, you can refer to the JSON tutorial.

In Java, we can use a library called json.simple to handle JSON data. So, using this, we can easily convert a JSON string into a Java object.

Why Convert JSON String to Java Object?

There are many reasons to convert a JSON string to a Java object. Let's take a look at some of them:

  • JSON is a common way to share data, especially in web services. It is made easier to use in Java programs by converting it into Java objects.
  • Java objects are obviously easier to work with in Java code compared to plain JSON text.
  • Changing JSON into Java objects helps us to find the mistakes and errors.
  • Java objects can be saved and loaded easily, which makes it easy to manage data.
  • Using Java objects, it works better with other Java tools and libraries.

Let's take a scenario where we have a JSON string, and we need to convert it into a Java object.

Suppose you have a file that has student information in JSON format. You need the data in Java object form because you want to use it in your Java program in order to use the data for some functionality. Here, you can't directly use the JSON string, so you need to convert it into a Java object.

For example, you have a JSON string that contains information about a student, including their name, age, and grade. You need to convert this JSON string into a Java object so that you can work with it in your Java program.

Here is an example of a JSON string:

{"name":"Ansh","age":22,"grade":"A"}

And here is the equivalent Java object:

public class Student{
   private String name;
   private int age;
   private String grade;

   // 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;
   }
   public String getGrade() {
      return grade;
   }
   public void setGrade(String grade) {
      this.grade = grade;
   }
}

In this example, the JSON string contains a student's name, age, and grade. The Java object represents the same information using fields and methods. Now, let's see how to convert a JSON string to a Java object using the json.simple library.

Convert JSON String to Java Object using json.simple

To convert a JSON string to a Java object using the json.simple library, follow these steps:

Add the json.simple library: You can download the library from its official website or simply include it in your project if you are using a tool like Maven or Gradle. If you are using Maven, add this to your pom.xml file:

<dependency>
   <groupId>com.googlecode.json-simple</groupId>
   <artifactId>json-simple</artifactId>
   <version>1.1.1</version>
</dependency>

Now, let's take an example JSON string and convert it into a Java object using the json.simple library.

In this example, we created a JSON string that contains information about a student. We then used the json.simple library to parse the JSON string and convert it into a Java object. Finally, we printed the values of the fields in the Java object.

Here is the program to convert a JSON string to a Java object:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;

public class ConvertJSONStringToObjectTest {
   public static void main(String[] args) {
      String jsonString = "{"name":"Ansh","age":22,"grade":"A"}";
      JSONParser parser = new JSONParser();
      JSONObject obj;
      try {
         obj = (JSONObject)parser.parse(jsonString);
         System.out.println("Name: " + obj.get("name"));
         System.out.println("Age: " + obj.get("age"));
         System.out.println("Grade: " + obj.get("grade"));
      } catch(ParseException e) {
         e.printStackTrace();
      }
   }
}

Following is the output of the above code:

Name: Ansh
Age: 22
Grade: A

Run the above code in your Java environment, where you have installed the json.simple library, and you will see the output as shown above.

In this way, you can convert a JSON string to a Java object using the json.simple library.

Updated on: 2025-04-21T19:35:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements