Gson – Fix Expected BEGIN_OBJECT but was BEGIN_ARRAY example shows how to fix com.google.gson.JsonSyntaxException: java.lang.IllegalStateException exception while parsing JSON using the Gson library in Java.
What causes this exception?
This exception occurs when the JSON data contains a JSON array but we try to parse it as a JSON object. Consider the below given JSON.
1 2 3 4 5 6 7 8 9 10 |
[ { "id": 1, "name": "Bob" }, { "id": 2, "name": "Raj" } ] |
And here is the Java code to parse/read it using Gson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; public class GsonBeginObjectException { public static void main(String[] args) { String jsonData = "[{\"id\": 1,\"name\": \"Bob\"},{\"id\": 2,\"name\": \"Raj\"}]"; Student student = new Gson().fromJson(jsonData, Student.class); System.out.println(student.getId() + " => " + student.getName()); } } class Student{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:397) at com.google.gson.Gson.fromJson(Gson.java:1227) at com.google.gson.Gson.fromJson(Gson.java:1137) at com.google.gson.Gson.fromJson(Gson.java:1047) at com.google.gson.Gson.fromJson(Gson.java:982) at com.javacodeexamples.gsonexamples.GsonBeginObjectException.main(GsonBeginObjectException.java:11) Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:393) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:386) ... 5 more |
Solution:
As we can see from the output, the code throws com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY exception.
The reason is, our JSON contains an array of Student objects, not an individual object. But the code tries to parse it as an object. In short, when we try to parse a JSON array as an object instead of an array, Gson throws this exception.
The correct way is to parse the JSON array into a Java array is as given below.
1 2 3 4 5 6 7 |
String jsonData = "[{\"id\": 1,\"name\": \"Bob\"},{\"id\": 2,\"name\": \"Raj\"}]"; Student[] students = new Gson().fromJson(jsonData, Student[].class); for(Student student : students) { System.out.println(student.getId() + " => " + student.getName()); } |
Output
1 2 |
1 => Bob 2 => Raj |
We can also parse JSON array to a Java list as given below.
1 2 3 4 5 6 7 8 9 |
String jsonData = "[{\"id\": 1,\"name\": \"Bob\"},{\"id\": 2,\"name\": \"Raj\"}]"; Type studentListType = new TypeToken<List<Student>>(){}.getType(); List<Student> studentList = new Gson().fromJson(jsonData, studentListType); for(Student student : studentList) { System.out.println(student.getId() + " => " + student.getName()); } |
Output
1 2 |
1 => Bob 2 => Raj |
Please let me know your views in the comments section below.