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
Michael MMichael M 

Help with JSON.deserialzeuntyped parsing

I am parsing JSON that is coming back from  REST callout, and when running, it is throwing this error: attempt to de-reference null object. The issue seems to be with how I am parsing the JSON, but I am not sure where I am going wrong. Can someone help?

Here is the snippet of the JSON I am parsing:
"result": {
"first_name": "JOHN",
"last_name": "SMITH",
"id_type": "MI",
"subscriber_id": "1234567",
"gender": "M",
"birthdate": "19601119",
"address": {
"info": "123 Main St.",
"city": "New York",
"state": "NY",
"zip_code": "100000"
},
"county:": {
"value": "14"
},
"office": {
"value": "H78"
},
"recert_month": {
"value": "12",
"name": "December"
},
"dates": [
{
"name": "Service",
"qualifier": "472",
"date": "2020-09-29"
},
{
"name": "Plan Begin",
"qualifier": "346",
"date": "2020-09-01"
},
{
"name": "Issue",
"qualifier": "102",
"date": "2020-06-01"
}
],


Here is how I am parsing it (bold part is causing error):
  
        Map<String, Object> wholeResponse = (Map<String, Object>) JSON.deserializeUntyped(response2.getBody());
            Map<String, Object> result = (Map<String, Object>) wholeResponse.get('result');
                Map<String, Object> residentAddress = (Map<String, Object>) result.get('address');
                   Map<String, Object> county = (Map<String, Object>) result.get('county');
                  Map<String, Object> office = (Map<String, Object>) result.get('office');
                 Map<String, Object> recert_month = (Map<String, Object>) result.get('recert_month');
        
       Eligibility__c newEligRec = new Eligibility__c(); 
        newEligRec.Referral_Name__c = ref.id;
        newEligRec.First_Name__c = string.valueof(result.get('first_name'));
        newEligRec.Last_Name__c = string.valueof(result.get('last_name'));
        newEligRec.Name_Id_Type__c = string.valueof(result.get('id_type'));
        newEligRec.Client_ID__c = string.valueOf(result.get('subscriber_id'));
        newEligRec.Gender__c = string.valueOf(result.get('gender'));
        string birthdate;
              if (string.valueof(result.get('birthdate')) != null){
                  birthdate = string.valueof(result.get('birthdate')).remove('-');
                  string year = birthdate.substring(0,4);
                  string month = birthdate.substring(4,6);
                  string day = birthdate.substring(6,8);
                 birthdate = month + '/' + day + '/' + year;
                    }
        newEligRec.Date_of_Birth__c = birthdate;
        newEligRec.Address_Number_and_Street__c = string.valueOf(residentAddress.get('info')); 
        newEligRec.City__c =  string.valueOf(residentAddress.get('city'));    
        newEligRec.State__c =  string.valueOf(residentAddress.get('state'));    
        newEligRec.Zip_Code__c = string.valueOf(residentAddress.get('zip_code'));
        newEligRec.County__c = string.valueOf(county.get('value'));
        newEligRec.Office__c = string.valueOf(office.get('value'));
        newEligRec.Recertification_Month_value__c = string.valueOf(recert_month.get('value'));
        newEligRec.Recertification_Month__c = string.valueOf(recert_month.get('name'));
Best Answer chosen by Michael M
Maharajan CMaharajan C
HI Michael,

The problem is : we have extra colon in county on JSON with in the double quotes .  So it's causing the attempt to de-reference null.
See the extra colon inside county double quotes -->  "county:" : { "value": "14" }  .

So change the below line: Add the extra colon in county string like below. This change will resolve your issue.
Map<String, Object> county = (Map<String, Object>) wholeResponse.get('county:');

Other wise the external system hava to remove this colon from the JSON.

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
HI Michael,

The problem is : we have extra colon in county on JSON with in the double quotes .  So it's causing the attempt to de-reference null.
See the extra colon inside county double quotes -->  "county:" : { "value": "14" }  .

So change the below line: Add the extra colon in county string like below. This change will resolve your issue.
Map<String, Object> county = (Map<String, Object>) wholeResponse.get('county:');

Other wise the external system hava to remove this colon from the JSON.

Thanks,
Maharajan.C
This was selected as the best answer
Michael MMichael M
Omg thank you! I did not notice that!! 

If you don't mind me adding to this thread, (this is my first time parsing JSON :)) what is the best way to parse this next part:

"dates": [
{
"name": "Service",
"qualifier": "472",
"date": "2020-09-29"
},
{
"name": "Plan Begin",
"qualifier": "346",
"date": "2020-09-01"
},
{
"name": "Issue",
"qualifier": "102",
"date": "2020-06-01"
}
],
Maharajan CMaharajan C
Try like below:
 
List<Object> lstdates = (List<Object>)result.get('dates');

system.debug('lstdates ==> ' + lstdates);

for (Object dt : lstdates) {
     Map<String, Object> dateattr = (Map<String, Object>)dt;
     System.debug( ' name --> ' + string.valueOf(dateattr.get('name')) );
     System.debug( ' qualifier --> ' + string.valueOf(dateattr.get('qualifier')) );
     System.debug( ' date --> ' + string.valueOf(dateattr.get('date')) );
 }

Thanks,
Maharajan.C

 
Michael MMichael M
Thank you so much. And so in this case there are 3 maps in the "Dates" string. How would I get the values from each map individually? 
Michael MMichael M
I mean "Dates" list. 
Michael MMichael M
I have also asked the question in a new thread so don't need to bother you with it. Thank you so much for all your help, i really appreciate it.