Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

When working with JSON in Java using the Gson library, we have several options at our disposal for converting raw JSON into other classes or data structures that we can work with more easily.

For example, we can convert JSON strings to a Map<String, Object> or create a custom class with mappings. However, sometimes it’s handy to be able to convert our JSON into a generic object.

In this tutorial, we’ll learn how Gson can give us a JsonObject from a String.

2. Maven Dependency

First, we’ll need to include the gson dependency in our pom.xml:

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

We can find the latest version of gson on Maven Central.

3. Using JsonParser

The first approach we’ll examine for converting a JSON String to a JsonObject is a two-step process that uses the JsonParser class.

For the first step, we need to parse our original String.

Gson provides us with a parser called JsonParser, which parses the specified JSON String into a parse tree of JsonElements:

public static JsonElement parseString(String json) throws JsonSyntaxException

Once we have our String parsed in a JsonElement tree, we’ll use the getAsJsonObject() method, which will return the desired result.

Let’s see how we get our final JsonObject:

String json = "{ \"name\": \"Baeldung\", \"java\": true }";

JsonObject jsonObject = JsonParser.parseString(json)
    .getAsJsonObject();

assertTrue(jsonObject.isJsonObject());
assertEquals("Baeldung", jsonObject.get("name")
    .getAsString());
assertTrue(jsonObject.get("java")
    .getAsBoolean());

4. Using fromJson 

In our second approach, we’ll see how to create a Gson instance and use the fromJson method. This method deserializes the specified JSON String into an object of the specified class:

public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException

Let’s see how we can use this method to parse our JSON String, passing the JsonObject class as the second parameter:

String json = "{ \"name\": \"Baeldung\", \"java\": true }";

JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);

assertTrue(convertedObject.isJsonObject());
assertEquals("Baeldung", convertedObject.get("name")
    .getAsString());
assertTrue(convertedObject.get("java")
    .getAsBoolean());

5. Conclusion

In this brief article, we learned two different ways to use the Gson library to get a JsonObject from a JSON-formatted String in Java. We should use the one that fits better with our intermediate JSON operations.

As usual, the source code for these examples is available over on GitHub.

Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.