[fix] JSON Expected BEGIN_OBJECT but was STRING at line 1 column 1


Stack Trace:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
 at com.google.gson.Gson.fromJson(Gson.java:810)
 at com.google.gson.Gson.fromJson(Gson.java:775)
 at com.google.gson.Gson.fromJson(Gson.java:724)
 at com.google.gson.Gson.fromJson(Gson.java:696)
 at JsonToGsonExample.main(JsonToGsonExample.java:52)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $


JsonSyntaxException is when you are trying to parse a JSON String and it is not well formatted. BEGIN_OBJECT is the start of a JSON object and it should be open curly braces {, which is surely missing

Fix:
  • Add the missing { for the JSON String
  • If the error is not on line 1 column 1, just follow the line and column to reach to the location within your JSON and add the missing curly brace.
  • Make sure your JSON String is valid. You may make use of Online JSON Validators (careful not to have sensitive data)


Before fix: "name":"ABC", "age":22}

Ater fix: { "name":"ABC", "age":22}

Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.