function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prasanth sfdcprasanth sfdc 

Malformed JSON: Expected '[' at the beginning of List/Set (REST API)

I am calling the below class from the lighting component OR form devloper console and i am getting the same error in the debug log. Kindly help me to sort this. Response body is coming and when I am deserilizing the error is coming.  please help to sort this.

Debug log error message.
public class ApiCallLightningComponent {
 /*
 * @Name : - fetchUserSessionId
 * @Description: - Call the VF page and get the Log In Use Session Id
 * @Params : - none
 * @ReturnType : - String
 */
 public static String fetchUserSessionId(){
 String sessionId = '';
 // Refer to the Page
 PageReference reportPage = Page.GetSessionIdVF;
 // Get the content of the VF page
 String vfContent = reportPage.getContent().toString();
 System.debug('vfContent '+vfContent);
 // Find the position of Start_Of_Session_Id and End_Of_Session_Id
 Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
 endP = vfContent.indexOf('End_Of_Session_Id');
 // Get the Session Id
 sessionId = vfContent.substring(startP, endP);
 System.debug('sessionId '+sessionId);
 // Return Session Id
 return sessionId;
 }
 /*
 * @Name - makeAPICall
 * @Description - Responsible for making API Call out
 * @params - None
 * @ReturnType - String
 */
 @AuraEnabled
 public static list<accountwrapper> makeAPICall(){
 String sessionId = fetchUserSessionId();
 HTTP h = new HTTP();
 HTTPRequest req = new HTTPRequest();
 HttpResponse resp = new HttpResponse();
 req.setMethod('GET');
 req.setHeader('Authorization', 'Bearer ' + sessionId);
     system.debug('coming here 1');
 req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v41.0/query?q=Select+Id,+Name+From+Account');
 resp = h.send(req);
 System.debug('#### Response Status '+resp.getStatus());
 System.debug('#### Response Status Code '+resp.getStatusCOde());
 System.debug(resp.getBody());
    list<accountwrapper> accountwrapperList = new list<accountwrapper>();
    //  string temp = string.valueof(resp.getBody()).substringAfter('"records":');
      system.debug('the response:'+resp.getBody());
    // system.debug('the temp is:'+temp);
     accountwrapperList = (list<accountwrapper>)system.json.deserialize(resp.getBody(), list<accountwrapper>.class);
     // return JSON.serialize(resp.getBody());
     system.debug('The accountwrapperList is :'+accountwrapperList);
      return accountwrapperList;
 } 
    
    public class accountwrapper{
       @AuraEnabled public string Id;
       @AuraEnabled public string Name;
    }
}

 
v varaprasadv varaprasad
Hi Prashanth,

Please check once below link:
https://salesforce.stackexchange.com/questions/75622/deserialize-json-array-with-integer-index

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: salesforcestart1@gmail.com

Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1