[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}



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap