Using Gson Library
Gson is a Java library developed by Google that can be used to convert Java objects into their JSON representation & vice versa. It provides simple methods to parse JSON strings into Java objects.
To use Gson for converting a JSON string to a JSON object, we need to follow these steps:
1. Add the Gson dependency to your project. If you're using Maven, include this in your pom.xml file:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
2. Import the required Gson classes in your Java file
import com.google.gson.Gson;
import com.google.gson.JsonObject;
3. Create a Gson object
Gson gson = new Gson();
4. Use the fromJson() method of the Gson object to parse the JSON string into a JsonObject
String jsonString = "{\"name\":\"Rahul\", \"age\":25, \"city\":\"Delhi\"}";
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
5. Now you can access the values from the JsonObject using the get() method
String name = jsonObject.get("name").getAsString();
int age = jsonObject.get("age").getAsInt();
That’s it, it's done. It’s very easy to execute and learn.
Using JSON-Simple Library
JSON-Simple is another popular Java library that provides a simple way to parse JSON data. It allows you to convert a JSON string into a JSONObject, which is a collection of key/value pairs.
Here's how you can use the JSON-Simple library to convert a JSON string to a JSON object:
1. Add the JSON-Simple dependency to your project. For Maven, include this in your pom.xml file:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
2. Import the required classes from the JSON-Simple library
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
3. Create a JSONParser object
JSONParser parser = new JSONParser();
4. Use the parse() method of the JSONParser to parse the JSON string into a JSONObject
String jsonString = "{\"name\":\"Rinki\", \"age\":22, \"city\":\"Mumbai\"}";
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
5. You can now access the values from the JSONObject using the get() method
String name = (String) jsonObject.get("name");
long age = (long) jsonObject.get("age");
As you can see, using the JSON-Simple library is also very easy to execute. It provides a simple & efficient way to convert JSON strings into JSON objects.
Jackson Library
Jackson is a high-performance JSON processor for Java. It provides a simple way to convert JSON strings into Java objects & vice versa. Jackson is widely used due to its efficiency & advanced features.
To use Jackson for converting a JSON string to a JSON object, we have to follow below mentioned steps:
1. Add the Jackson dependency to your project. For Maven, include this in your pom.xml file
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
2. Import the required Jackson classes
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
3. Create an ObjectMapper object
ObjectMapper mapper = new ObjectMapper();
4. Use the readTree() method of the ObjectMapper to parse the JSON string into a JsonNode object
String jsonString = "{\"name\":\"Harsh\", \"age\":30, \"city\":\"Bangalore\"}";
JsonNode jsonNode = mapper.readTree(jsonString);
5. You can now access the values from the JsonNode using the get() method
String name = jsonNode.get("name").asText();
int age = jsonNode.get("age").asInt();
Jackson provides a convenient way to work with JSON data in Java. It offers advanced features like data binding, streaming API & tree model for handling JSON.
JSON String to JSON Object Conversion Example
Now that we have learned about different libraries for converting JSON strings to JSON objects, let's see a complete code example that demonstrates this process.
Here's a Java program that converts a JSON string to a JSON object using the Gson library:
Java
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class JsonStringToObjectExample {
public static void main(String[] args) {
// JSON string
String jsonString = "{\"name\":\"Sanjana\", \"age\":27, \"city\":\"Hyderabad\", \"hobbies\":[\"reading\", \"traveling\"]}";
// Create Gson object
Gson gson = new Gson();
// Parse JSON string to JSON object
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
// Access values from JSON object
String name = jsonObject.get("name").getAsString();
int age = jsonObject.get("age").getAsInt();
String city = jsonObject.get("city").getAsString();
// Print the values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
// Access array values from JSON object
JsonArray hobbies = jsonObject.getAsJsonArray("hobbies");
System.out.println("Hobbies:");
for (JsonElement hobby : hobbies) {
System.out.println(hobby.getAsString());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Name: Sanjana
Age: 27
City: Hyderabad
Hobbies:
reading
traveling
In this example, we have a JSON string that contains information about a person, including their name, age, city & hobbies. We create a Gson object & use its fromJson() method to parse the JSON string into a JsonObject.
We can then access the values from the JsonObject using the get() method & appropriate getAsXXX() methods based on the data type. We print the retrieved values to the console.
The example also shows how to access array values from the JSON object using the getAsJsonArray() method & iterating over the elements.
Frequently Asked Questions
Can I use any of these libraries to convert a JSON object back to a JSON string?
Yes, all the libraries mentioned (Gson, JSON-Simple & Jackson) provide methods to convert JSON objects back to JSON strings.
Do I need to add any specific exception handling while parsing JSON strings?
Yes, it's recommended to handle exceptions like JsonParseException or IOException that may occur during the parsing process.
Is there any performance difference between these JSON parsing libraries?
Jackson is known for its high performance compared to Gson & JSON-Simple. However, the choice of library depends on your specific requirements & project constraints.
Conclusion
In this article, we learned how to convert a JSON string to a JSON object in Java using three popular libraries: Gson, JSON-Simple & Jackson. We explained the steps involved in using each library & saw a complete code example showing the conversion process.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.