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

In this tutorial, we’ll discuss some common Gson errors and how to fix them.

2. The Gson Library

Gson is a Java library developed by Google that allows converting Java objects to JSON and vice versa. It simplifies serialization and deserialization tasks, providing customization options and efficient performance for handling complex object structures.

The latest version of the Gson library can be found in the Maven Central Repository. Let’s add the dependency to our pom.xml:

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

3. Get an Array While Expecting an Object

First, let’s define a simple Person object with a lone name attribute:

public class Person {

    public String name;

    // standard constructor and getter

}

Let’s now consider a JSONArray of two persons named James and John:

[
  {
    "name": "John"
  },
  {
    "name": "James"
  }
]

What if we try by mistake to parse this JSONArray into a simple Person object? We’d do this via the following instruction:

Person person = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", Person.class);

When running this code, we get the following Exception:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

It’s indeed impossible to parse a JSONArray into a lone Object. To fix this error, we should parse it into an array or a Collection instead. Parsing into an array is very straightforward:

Person[] personArray = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", Person[].class)

However, parsing into a Collection requires some preliminary work. We need to define the target Type beforehand:

Type collectionType = new TypeToken<Collection<Person>>() {}.getType();
Collection<Person> personCollection = new Gson().fromJson("[{\"name\":\"John\"},{\"name\":\"James\"}]", collectionType);

4. Get an Object While Expecting an Array

On the other hand, let’s now define the following Person named John:

{
  "name": "John"
}

Let’s try to parse this JSONObject into a Java array:

Person[] personArray = new Gson().fromJson("{\"name\":\"John\"}", Person[].class);

This code throws the following JsonSyntaxException:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

The error message indicates to us that we should parse the input into a lone Object:

Person person = new Gson().fromJson("{\"name\":\"John\"}", Person.class);

5. Conclusion

In this article, we learned about common mistakes made while using Gson for object deserialization. Incorrectly trying to match an array with an Object, or the contrary, will always lead to an error.

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