+ Start a Discussion
RarLopzRarLopz 

Debug logs: Wrapper returning null

As seen in debug logs , why is the string json on deserializing returning null ? 
 
{
    "caPerformanceRecordList": [
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-01",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "ECKNWNSPT",
            "submissionCount": 2
        },
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-02",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "RTEONE",
            "submissionCount": 15
        }
    ]
}


public with sharing class CASReportController {

		public list<JSONWrapperController> wrapper {get;set;}

		public void deserialize() {
		
		string jsonresponse = '{"caPerformanceRecordList":[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]}';
		  
		 system.debug('json string is ' +jsonresponse);
		 wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, lis<JSONWrapperController>.class);
        
        system.debug('This is the wrapper class: ' +wrapper);
	}
}



public class JSONWrapperController {
	
	    public class CaPerformanceRecordList {   
	    	
		public string dealerName {get; set;}
	        public String motorDealerId {get;set;}
	        public DateTime mtd {get; set;}
	        public Integer submissionCount{get;set;}
		public String idlPlus{get;set;}
		public String creditApplicationSourceCode{get;set;}
		    
    	}
	
	 public List<CaPerformanceRecordList> caperfreclist {get;set;} 
   
}


DEBUG|This is the wrapper class: JSONWrapperController:[caperfreclist=null]

 
Best Answer chosen by RarLopz
Deepali KulshresthaDeepali Kulshrestha
Hi RarLopz,
Greetings to you!

- I read your problem and implemented in my Org.
- Your code does not have all the fields which are available in JSON.
 
- First correction : -  

    wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, list<JSONWrapperController>.class);
    
- Second Correction : - 

    public class CaPerformanceRecordList {

        public string dealerName {get; set;}
        public String motorDealerId {get;set;}
        public Date reportingPeriod {get; set;}
        public Integer submissionCount{get;set;}
        public String idlPlus{get;set;}
        public String creditApplicationSourceCode{get;set;}
        public String financeSourceIntegrationTypeCode{get;set;}

    }

Third Correction: - JSON should be like this for deserialize: - 

    [
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-01",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "ECKNWNSPT",
            "submissionCount": 2
        },
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-02",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "RTEONE",
            "submissionCount": 15
        }
    ]
    
 Please use the below code for wrapper[Solved] : - 

    public with sharing class CASReportController {

        public list<JSONWrapperController> wrapper {get;set;}

        public void deserialize() {
            try{
                string jsonresponse = '[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },' +
                        '{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },' +
                        '{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]';

                system.debug('json string is ' +jsonresponse);
                wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, list<JSONWrapperController>.class);

                system.debug('This is the wrapper class: ' +wrapper);
            }catch (Exception ee){
                System.debug('Eror-->'+ee.getLineNumber()+ee.getMessage());
            }
        }

        public class JSONWrapperController {

                public string dealerName {get; set;}
                public String motorDealerId {get;set;}
                public Date reportingPeriod {get; set;}
                public Integer submissionCount{get;set;}
                public String idlPlus{get;set;}
                public String creditApplicationSourceCode{get;set;}
                public String financeSourceIntegrationTypeCode{get;set;}

        }
    }


    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
 

All Answers

Ajay K DubediAjay K Dubedi
Hi RarLopz,
Use below code it will help you:

public class CASReportController {

    public class CaPerformanceRecordList {
        public String motorDealerID {get;set;} 
        public String dealerName {get;set;} 
        public String reportingPeriod {get;set;} 
        public String financeSourceIntegrationTypeCode {get;set;} 
        public String idlPlus {get;set;} 
        public String creditApplicationSourceCode {get;set;} 
        public Integer submissionCount {get;set;} 

        public CaPerformanceRecordList(JSONParser parser) {
            while (parser.nextToken() != System.JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                        if (text == 'motorDealerID') {
                            motorDealerID = parser.getText();
                        } else if (text == 'dealerName') {
                            dealerName = parser.getText();
                        } else if (text == 'reportingPeriod') {
                            reportingPeriod = parser.getText();
                        } else if (text == 'financeSourceIntegrationTypeCode') {
                            financeSourceIntegrationTypeCode = parser.getText();
                        } else if (text == 'idlPlus') {
                            idlPlus = parser.getText();
                        } else if (text == 'creditApplicationSourceCode') {
                            creditApplicationSourceCode = parser.getText();
                        } else if (text == 'submissionCount') {
                            submissionCount = parser.getIntegerValue();
                        } else {
                            System.debug(LoggingLevel.WARN, 'CaPerformanceRecordList consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }
    
    public List<CaPerformanceRecordList> caPerformanceRecordList {get;set;} 

    public CASReportController(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'caPerformanceRecordList') {
                        caPerformanceRecordList = arrayOfCaPerformanceRecordList(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'CASReportController consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
    public static CASReportController parse(String json) {
        System.JSONParser parser = System.JSON.createParser(json);
        return new CASReportController(parser);
    }
    
    public static void consumeObject(System.JSONParser parser) {
        Integer depth = 0;
        do {
            System.JSONToken curr = parser.getCurrentToken();
            if (curr == System.JSONToken.START_OBJECT || 
                curr == System.JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == System.JSONToken.END_OBJECT ||
                curr == System.JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }
    private static List<CaPerformanceRecordList> arrayOfCaPerformanceRecordList(System.JSONParser p) {
        List<CaPerformanceRecordList> res = new List<CaPerformanceRecordList>();
        if (p.getCurrentToken() == null) p.nextToken();
        while (p.nextToken() != System.JSONToken.END_ARRAY) {
            res.add(new CaPerformanceRecordList(p));
        }
        return res;
    }
}

//Execute the method:

string jsonresponse = '{"caPerformanceRecordList":[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]}';
System.debug('Result : '+CASReportController.parse(jsonresponse));          

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi        
        
Deepali KulshresthaDeepali Kulshrestha
Hi RarLopz,
Greetings to you!

- I read your problem and implemented in my Org.
- Your code does not have all the fields which are available in JSON.
 
- First correction : -  

    wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, list<JSONWrapperController>.class);
    
- Second Correction : - 

    public class CaPerformanceRecordList {

        public string dealerName {get; set;}
        public String motorDealerId {get;set;}
        public Date reportingPeriod {get; set;}
        public Integer submissionCount{get;set;}
        public String idlPlus{get;set;}
        public String creditApplicationSourceCode{get;set;}
        public String financeSourceIntegrationTypeCode{get;set;}

    }

Third Correction: - JSON should be like this for deserialize: - 

    [
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-01",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "ECKNWNSPT",
            "submissionCount": 2
        },
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-02",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "RTEONE",
            "submissionCount": 15
        }
    ]
    
 Please use the below code for wrapper[Solved] : - 

    public with sharing class CASReportController {

        public list<JSONWrapperController> wrapper {get;set;}

        public void deserialize() {
            try{
                string jsonresponse = '[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },' +
                        '{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },' +
                        '{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]';

                system.debug('json string is ' +jsonresponse);
                wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, list<JSONWrapperController>.class);

                system.debug('This is the wrapper class: ' +wrapper);
            }catch (Exception ee){
                System.debug('Eror-->'+ee.getLineNumber()+ee.getMessage());
            }
        }

        public class JSONWrapperController {

                public string dealerName {get; set;}
                public String motorDealerId {get;set;}
                public Date reportingPeriod {get; set;}
                public Integer submissionCount{get;set;}
                public String idlPlus{get;set;}
                public String creditApplicationSourceCode{get;set;}
                public String financeSourceIntegrationTypeCode{get;set;}

        }
    }


    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
 
This was selected as the best answer
Piyush Gautam 6Piyush Gautam 6
Hi RarLopz,

Please follow the below code:
Everything was correct except some simple things that have been highlighted
public with sharing class CASReportController {

		public JSONWrapperController wrapper {get;set;}// Not a List but a complete Object

		public void deserialize() {
		
		string jsonresponse = '{"caPerformanceRecordList":[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]}';
		  
		 system.debug('json string is ' +jsonresponse);
		 wrapper = (JSONWrapperController) JSON.deserialize(jsonresponse, JSONWrapperController.class);
        
        system.debug('This is the wrapper class: ' +wrapper);
	}
}



public class JSONWrapperController {
    
    public List<CaPerformanceRecord> CaPerformanceRecordList {get;set;}// Name should be identical as the node in response
	
    public class CaPerformanceRecord {
        
        public String motorDealerID {get;set;}
        public string dealerName {get; set;}
        public string reportingPeriod{get; set;}
        public string financeSourceIntegrationTypeCode{get; set;}
        public string idlPlus{get; set;}
        public string creditApplicationSourceCode{get; set;}
        public string submissionCount{get; set;}
       
    } 
 
}


Hope you find helpful, Please mark as solved.
RarLopzRarLopz
Thank you to @Piyush Gautam 6, @Deepali Kulshrestha, @Ajay K Dubedi

This is what i finally hav and was able to see the JSON String cast to  the JSONWrapperController


Posting my code here for someone who might find it helpful. 

 
This is a snapshot of the actual JSON Response from Endpoint as obtained from Postman: 

{
    "caPerformanceRecordList": [
        
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-05-18",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "STEONE",
            "submissionCount": 2
        },
        {
            "motorDealerID": "XL1HX",
            "dealerName": "Subaru Auto Dealer",
            "reportingPeriod": "2019-06-27",
            "financeSourceIntegrationTypeCode": "FIFS",
            "idlPlus": "Y",
            "creditApplicationSourceCode": "STEONE",
            "submissionCount": 5
        }
    ],
    "ecPerformanceRecordList": [
        {
            "motorDealerID": "XL1HX",
            "submitUserID": "BCUSER1",
            "bookedDate": "2019-05-22 00:00:00.0",
            "ecEligible": "Y",
            "caBookedCount": 1,
            "ecBookedCount": 0
        },
        {
            "motorDealerID": "XL1HX",
            "submitUserID": "BCUSER1",
            "bookedDate": "2019-06-05 00:00:00.0",
            "ecEligible": "Y",
            "caBookedCount": 1,
            "ecBookedCount": 0
        }
    ]
}

 I copied this and pasted it as a string to be able to test if it wraps correctly to my JSONWrapperController class . Based on suggestions from above mentioned SFDC community members, this is my classr that that a method which will be eventually called by the Visualforce page 

 
public with sharing class CASReportController {
@AuraEnabled
		public list<JSONWrapperController> wrapper {get;set;}
		
		public void deserialize() {
		
			try{
				string jsonresponse = '[{ "caPerformanceRecordList": [ { "motorDealerID": "XL1HX", "dealerName": "Subaru Auto Dealer", "reportingPeriod": "2019-05-18", "financeSourceIntegrationTypeCode": "FIFS", "idlPlus": "Y", "creditApplicationSourceCode": "STEONE", "submissionCount": 2 }, { "motorDealerID": "XL1HX", "dealerName": "Subaru Auto Dealer", "reportingPeriod": "2019-06-27", "financeSourceIntegrationTypeCode": "FIFS", "idlPlus": "Y", "creditApplicationSourceCode": "STEONE", "submissionCount": 5 } ], "ecPerformanceRecordList": [ { "motorDealerID": "XL1HX", "submitUserID": "BCUSER1", "bookedDate": "2019-05-22 00:00:00.0", "ecEligible": "Y", "caBookedCount": 1, "ecBookedCount": 0 }, { "motorDealerID": "XL1HX", "submitUserID": "BCUSER1", "bookedDate": "2019-06-05 00:00:00.0", "ecEligible": "Y", "caBookedCount": 1, "ecBookedCount": 0 } ] }]';
			  
			 	system.debug('json string is ' +jsonresponse);
			 	wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, list<JSONWrapperController>.class);
			 
		     	system.debug('This is the wrapper class: ' +wrapper);
			 } catch (Exception e){
				system.debug('Error---->' +  e.getLineNumber() + e.getMessage());
				
			}
       }
}

I used the JSON2Apex tool to build the JSONWrappper Class.
 
public class JSONWrapperController {

     public List<CaPerformanceRecordList> caPerformanceRecordList{get;set;}
     public List<EcPerformanceRecordList> ecPerformanceRecordList{get;set;}	
     
     public class EcPerformanceRecordList {
		public String motorDealerID{get;set;}
		public String submitUserID{get;set;}
		public String bookedDate{get;set;}
		public String ecEligible{get;set;}
		public Integer caBookedCount{get;set;}
		public Integer ecBookedCount{get;set;}
	}

	

	public class CaPerformanceRecordList {
		public String motorDealerID{get;set;}
		public String dealerName{get;set;}
		public String reportingPeriod{get;set;}
		public String financeSourceIntegrationTypeCode{get;set;}
		public String idlPlus{get;set;}
		public String creditApplicationSourceCode;
		{get;set;}public Integer submissionCount;
	}

	
	
}