[SalesForce] Malformed JSON: Expected ‘[‘ at the beginning of List/Set\n\nClass.System.JSON.deserialize

Through the Http response I will get the output like below.

[{"CourseType":"OnlineCourse","Id":"05f932c9-1059-4495-87d9-2e7a09bd3930","Name":"Fundamentals
of Contact Lenses","Description":"This course
introduces","Notes":null,"ExternalId":null,"AccessDate":null,"ExpireType":0,"ExpireDuration":{"Years":0,"Months":0,"Days":0,"Hours":0},"ExpiryDate":null,"ActiveStatus":0,"TagIds":[],"ResourceIds":[],"EditorIds":[],"Prices":[],"CompetencyDefinitionIds":[],"PrerequisiteCourseIds":[],"PostEnrollmentCourseIds":[],"AllowCourseEvaluation":true,"CategoryId":null,"CertificateUrl":null,"Audience":"US","Goals":null,"Vendor":null,"CompanyCost":null,"LearnerCost":null,"CompanyTime":null,"LearnerTime":null},{…..},{……..}]

Assume that response will give more course types like OnlineCourse, offline and Curriculum. Just by using if condition, i tried to filter only course type = Curriculum.

if(CourseType == 'Curriculum')

Then I will get Id of the course type. By using the ID, I will again call the httpresponse and now I will only courses with the type Both. But after filtering, I am not getting the response in proper Json format. The response is coming like {…, …., …} which means the json format is missing '[ ]'.

If I can able to get the [ ] also, then I will add the response to wrapper class. So, the exception is coming while I am trying to add the result to wrapper class.

Below you find my try levels.

public static List<CurriculumObjId> FrCurriculumId(List<lmsUtils.CourseDTOFrUpdate> GrId)// Gr id contains only Curriculum course type
    {
        String sessionId;
        HttpResponse response;
        List<String> outString = new List<String>();
        List<CurriculumObjId> results;
        sessionId = lmsUtils.authenticate();

        for(lmsUtils.CourseDTOFrUpdate currId : GrId)
        {
            System.debug('Curriculum id$$$$$$$$$:'+currId.Id);
            String ep = LMS_Settings__c.getValues('EndPoint').Value__c+'v1/Curriculums?id='+currId.Id;

            if (!Test.isRunningTest())
            {
                response  = lmsUtils.processRequest(ep,'','GET',sessionId);
                System.debug('ResponseCode >>>>>>>'+response.getStatusCode());
                System.debug('GroupResponseBody >>>>>>>'+response.getBody());
                outString.add(response.getBody());
            }
            else
            {
                MockHttpResponseGenerator1 mc = new MockHttpResponseGenerator1();
                response = mc.respond2(); 
            }
        }
        System.debug('outString >>>>>>>'+outString);
        if(response.getStatusCode() == 200)
        {
            results = (List<CurriculumObjId>) JSON.deserialize(response.getBody(), List<CurriculumObjId>.class);
        }

        return results;
    }

    /* DTO for Curriculum Ids */
    Global class CurriculumObjId {

        public String CurriculumGroupIds{get;set;}
        public string TagIds{get;set;}
        public Date ExpiryDate{get;set;}
        public String Id{get;set;}
        public String Name{get;set;}

        public CurriculumObjId() {}
    }
    public class TagIds {
        string TagIds{get;set;}
    }
    public class CurriculumGroupIds {
        string TagIds{get;set;}
    }

Best Answer

Based on the response API and few suggestions from sfdcfox, I just changed from

 if(response.getStatusCode() == 200)
        {
            results = (List<CurriculumObjId>) JSON.deserialize(response.getBody(), List<CurriculumObjId>.class);
        }  

to

if(response.getStatusCode() == 200)
        {
            results = (urriculumObjId) JSON.deserialize(response.getBody(), CurriculumObjId.class);
        }

and the wrapper class should also need changes like below.

Global class CurriculumObjId {

        public List<String> CurriculumGroupIds{get;set;}
        public string TagIds{get;set;}
        public Date ExpiryDate{get;set;}
        public String Id{get;set;}
        public String Name{get;set;}

        public CurriculumObjId() {}
    }