0

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');
    finalRequest.setHeader('Content-Type','application/json');
    finalRequest.setHeader('accept','application/json');
    finalRequest.setMethod('GET');
    finalRequest.setEndpoint('https://name.salesforce.com/services/data/v44.0/query/?q=SELECT+Name+FROM+AnotherObject__c);
    return finalRequest;
}
//My code here
//and incorrect deserialization (Error is here):
accList = (List<Account>)JSON.deserialize(responseService.getBody(), List<Account>.class);
1
  • Hmm you're trying to deserialize something that isnt list of accounts into list of accounts? Nov 25, 2018 at 19:52

1 Answer 1

1

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;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .