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);
  • Hmm you're trying to deserialize something that isnt list of accounts into list of accounts? – user1974566 Nov 25 '18 at 19:52
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;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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