You need to sign in to do that
Don't have an account?

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'));
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'));
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
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
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"
}
],
Thanks,
Maharajan.C