Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Efficient JSON parsing is one of the most important tasks in Java programming when it comes to data manipulation and communication.

The Gson library offers a versatile JsonParser class to simplify the conversion process. Moreover, it’s important to note that this class has been deprecated, eliminating the need for instantiation. Instead, we can utilize the provided static methods for the conversion process.

In this tutorial, we’ll delve into how to utilize the static methods instead of the deprecated JsonParser for efficient JSON parsing in Java.

2. Deprecated JsonParser

Here is an example of using the deprecated JsonParser to parse a JSON string:

String jsonString = "{\"name\": \"John\", \"age\":30, \"city\":\"New York\"}";
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();

The deprecated JsonParser instance may still function, but developers are encouraged to move on with new and improved practices.

3. Embracing Static Methods

The Gson library offers static methods as replacements for the deprecated ones. Moreover, it is a more elegant and easier-to-understand parsing way of JSON.

Let’s explore the recommended static methods:

3.1. Parse from String

We can parse a JSON string directly into JsonObject without using a deprecated instance of JsonParser using the parseString() static method.

Firstly, let’s set up a JSON string describing person-related data and read an associated JsonObject with given keys like name, age, and city underlying class constructor of DeprecatedJsonParserUnitTest:

String jsonString = "{\"name\": \"John\", \"age\":30, \"city\":\"New York\"}";
JsonObject expectedJsonObject = new JsonObject();

DeprecatedJsonParserUnitTest() {
    expectedJsonObject.addProperty("name", "John");
    expectedJsonObject.addProperty("age", 30);
    expectedJsonObject.addProperty("city", "New York");
}

Now, let’s parse the jsonString directly into JsonObject:

@Test
public void givenJsonString_whenUsingParseString_thenJsonObjectIsExpected() {
    JsonObject jsonObjectAlt = JsonParser.parseString(jsonString).getAsJsonObject();
    assertEquals(expectedJsonObject, jsonObjectAlt);
}

In this test method, we verify that the parsed jsonObjectAlt matches the expectedJsonObject created earlier.

3.2. Parse from StringReader

There are cases when the obtained JSON data comes from a StringReader. We can use the parseReader() static method to get the same result without using obsolete components:

@Test
public void givenJsonString_whenUsingParseReader_thenJsonObjectIsExpected() {
    StringReader reader = new StringReader(jsonString);
    JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
    assertEquals(expectedJsonObject, jsonObject);
}

Here, we initialize a StringReader called reader. Then, we use the JsonParser.parseReader() method to parse the JSON data into a JsonObject.

3.3. Parse from JsonReader

When dealing with a JsonReader, the parseReader() static method is still an effective and contemporary decision that avoids outdated constructions. Let’s take an example:

@Test
public void givenJsonReader_whenParseUsingJsonReader_thenJsonObjectIsExpected() {
    JsonReader jsonReader = new JsonReader(new StringReader(jsonString));
    JsonObject jsonObject = JsonParser.parseReader(jsonReader).getAsJsonObject();
    assertEquals(expectedJsonObject, jsonObject);
}

In the above test method, we begin by instantiating a JsonReader named jsonReader with the contents of the JSON string. Then, we utilize the JsonParser.parseReader() method to parse such JSON data into a JsonObject.

4. Conclusion

In conclusion, JsonParser was deprecated, and there are excellent alternative static methods provided by the Gson class, such as parseString(), parseReader(), and parseJson().

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW