+ Start a Discussion
Jordan SchatzJordan Schatz 

JSON.deserialize works for a singe item, but not for a list

I am trying to deserialize some JSON into a custom class. If I deserlize just a single object, it works, but when wrapped in a list, it fails.

Here is an example:
public class attendanceWrapperClass {
    @AuraEnabled public Case eachContact{get;set;}
    @AuraEnabled public Attendance_Breakfast_Lunch_Snack__c eachAttendance{get;set;}
    @AuraEnabled public List<Attendance_Breakfast_Lunch_Snack__c> previousAttendanceRecordsList{get;set;}
}

string wrapperListItems = '[{"eachAttendance":{"Attendance_Code__c":"Y - Yes"},"eachContact":{"Client__c":"0036F00002xYORdQAO","Admission_Pathway_Site__c":"Wolverine Center","WHS_Treatment_Unit__c":"WC CMO Respite","WHS_Treatment_Program__c":"Wolverine Center Respite","Id":"5006F0000240Jv2QAE","Client__r":{"Name":"Cedric Russ","FirstName":"Cedric","LastName":"Russ","Id":"0036F00002xYORdQAO"}}}]';
list<attendanceWrapperClass> wrapperList = (List<attendanceWrapperClass>)JSON.deserialize(wrapperListItems,List<attendanceWrapperClass>.class);
system.debug(wrapperList);

The error I get is System.JSONException: Expected attendanceWrapperClass but found } at [line:1, column:369] basically it errors when the first item in the list ends.

What am I doing wrong?
Parmanand PathakParmanand Pathak
Hi Jordan,

Write the below class separately - 
public class MyParserClass{
		public eachContact eachContact{get;set;}
		public eachAttendance eachAttendance{get;set;}
	public class eachContact{
		public String Id{get;set;}
		public String WHS_Treatment_Unit__c{get;set;}
		public String Admission_Pathway_Site__c{get;set;}
		public String WHS_Treatment_Program__c{get;set;}
		public String Client__c{get;set;}
		public Client__r Client__r{get;set;}
	}
	public class eachAttendance{
		public String Attendance_Code__c{get;set;}
	}
	public class Client__r{
		public String Name{get;set;}
		public String LastName{get;set;}
		public String Id{get;set;}
		public String FirstName{get;set;}
	}
}

Use below code in your existing class - 
list<MyParserClass> wrapperList=(list<MyParserClass>)JSON.deserialize(wrapperListItems,list<MyParserClass>.class);
Let me know if it helps you or not.

Thanks,
Parmanand Pathak