Parse JSON Without Duplicate Keys Using Gson in Java



Parsing a JSON without duplicate keys means converting a JSON string into a Java object. But we need to keep in mind that the JSON string should not have duplicate keys.

Gson: Parsing a JSON Without Duplicate Keys

We can use the Gson library to parse a JSON without duplicate keys in Java. It is developed by Google and used for converting Java objects into JSON and vice versa.

By default, Gson does not allow duplicate keys in JSON. If a JSON string has duplicate keys, Gson will throw a JsonSyntaxException. We will use the methods fromJson() and toJson() to achieve our goal.

Let's learn how to parse a JSON without duplicate keys using the Gson library in Java. In order to use Gson library, we will need to add it to our project. If you are using Maven, add this to your pom.xml file:

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>

If you do not use Maven, you can download the jar file from here.

Steps 

Following are the steps to parse a JSON without duplicate keys using the Gson library:

  • Import the Gson library.
  • Create a class Person with fields for name and age.
  • Then create a constructor and getter methods for the fields.

  • Now, create a JSON string.
  • Convert the JSON string to a Person object using the fromJson() method of the Gson.
  • Print the Person object.
  • Use the toJson() method to convert the Person object back to a JSON string.
  • Print the JSON string.

Example

Following is the code to parse a JSON without duplicate keys using the Gson library:

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

public class ParseJsonWithoutDuplicateKeysExample {
   public static void main(String[] args) {
      // JSON string to be parsed
      String jsonString = "{"name":"Ansh","age":23}";
      // Create a Gson object
      Gson gson = new Gson();
      try {
         // Parse the JSON string to a Person object
         Person person = gson.fromJson(jsonString, Person.class);
         // Print the Person object
         System.out.println("Name: " + person.getName());
         System.out.println("Age: " + person.getAge());
         // Convert the Person object back to a JSON string
         String jsonOutput = gson.toJson(person);
         // Print the JSON string
         System.out.println("JSON Output: " + jsonOutput);
      } catch (JsonSyntaxException e) {
         System.out.println("Error parsing JSON: " + e.getMessage());
      }
   }
}
class Person {
   @SerializedName("name")
   private String name;
   @SerializedName("age")
   private int age;

   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }

   public String getName() {
      return name;
   }

   public int getAge() {
      return age;
   }
}

The output of the above code will be:

Name: Ansh
Age: 23
JSON Output: {"name":"Ansh","age":23}
Updated on: 2025-05-13T15:46:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements