Error: Malformed JSON: Expected '[' at the beginning of List/Set

I think I have incorrect JSON.deserialize for my object. How can I fix it? I don't use a wrapper.

List<Account> accList = new List<Account>();
public static HttpRequest createRequestForService(){
    HttpRequest finalRequest = new HttpRequest();
    finalRequest.setHeader('Authorization','Bearer ' + 'my_token_here');
    return finalRequest;
//My code here
//and incorrect deserialization (Error is here):
accList = (List<Account>)JSON.deserialize(responseService.getBody(), List<Account>.class);
  • Hmm you're trying to deserialize something that isnt list of accounts into list of accounts? – user1974566 Nov 25 '18 at 19:52

The REST API uses a wrapper itself, so you need to obey the rules. A proper response looks something like this:

public class RestResponseWrapper {
  public Integer totalSize;
  public Boolean done;
  public String nextRecordsUrl;
  public SObject[] records; // note: change to specific SObject type

You can read more about the query resource in the documentation.

accList = ((RestResponseWrapper)
  JSON.deserialize(responseService.getBody(), RestResponseWrapper.class)).records;

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.