0

I am trying to integrate salesforce and google books via rest api..But it is throwing the below error -

System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set

public class GoogleBooks {

        public class Items {
        public String kind {get;set;} 
        public String id {get;set;} 
        public String etag {get;set;} 
        public String selfLink {get;set;} 

    }

}

public class GoogleBooksResponse{
    String kind;
    Integer totalItems;
    List<GoogleBooks> items; //Your existing class.
}

And in the anonymous window i am running the below -

String s;
HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Accept','application/json');
        req.setEndpoint('https://www.googleapis.com/books/v1/volumes?q=harry+potter');
        Http http = new Http();
        HTTPResponse res = null;
        res = http.send(req);
         s = res.getBody();
List<GoogleBooksResponse> result = (List<GoogleBooksResponse>)JSON.deserialize(s , LIST<GoogleBooksResponse>.class);

I tried running the endpoint in ARC tool which gave me a big JSON response. When i validated the JSON, it showed some error.

**https://www.googleapis.com/books/v1/volumes?q=harry+potter**

Has anyone faced this issue.

3

The response you are getting is not a list. It is an object that has a property called "items", which contains a list of Google Books. You can fix this by creating another class with the properties that you see in the result:

https://www.googleapis.com/books/v1/volumes?q=harry+potter

For example you could deserialize the result into something like:

public class GoogleBooksResponse{
    String kind;
    Integer totalItems;
    List<GoogleBooks> items; //Your existing class.
}
  • Hi salesforce-hacker.. I have modified my class above.. But the error still persists.... – learnjqueery Feb 17 '18 at 19:41
  • 1
    You're still trying to deserialize a list. Change List<GoogleBooksResponse> to just GoogleBooksResponse. @salesforce-hacker's solution is the right one. – David Reed Feb 17 '18 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.