Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Jackson Top

Get started with Spring 5 and Spring Boot 2, 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.8.5</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 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 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.

Jackson bottom

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

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!