Skip to content

Gson – Solution: Expected BEGIN_ARRAY but was BEGIN_OBJECT

Gson – Solution: Expected BEGIN_ARRAY but was BEGIN_OBJECT example shows how to fix com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT exception while using the Gson in Java.

What causes this exception?

This exception is thrown by the Gson library when we try to parse JSON Object as a JSON array. Consider the below given JSON.

The above JSON contains a JSON object representing an employee. Let’s see what happens when we try to parse it as an array.

Output

Solution:

This exception is thrown by the Gson when we try to parse JSON object as JSON array. Here is how to correctly parse JSON object.

Output

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.