Jackson Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> 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.

Sometimes, however, it’s handy to be able to convert our JSON into a generic object. In this tutorial, we’ll see how Gson can give us a JsonObject from a String.

2. Maven Dependency

First of all, we need to include the gson dependency in our pom.xml:

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

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

3. Using JsonParser

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

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

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

public JsonElement parse(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 = new JsonParser().parse(json).getAsJsonObject();

Assert.assertTrue(jsonObject.isJsonObject());
Assert.assertTrue(jsonObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(jsonObject.get("java").getAsBoolean() == true);

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);

Assert.assertTrue(convertedObject.isJsonObject());
Assert.assertTrue(convertedObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(convertedObject.get("java").getAsBoolean() == true);

5. Conclusion

In this basic tutorial, we’ve learned two different ways to use the Gson library to get a JsonObject from a JSON-formatted String in Java. Consequently, 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.

Jackson bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

Leave a Reply

avatar
  Subscribe  
Notify of