0

I have an input json like the following. It has contact fields and a nested object for another contact. I'm using Postman to send requests.

{
  "Id": "0036D00000H24bHQAR",
  "Preferred_Name__c": "Preferred Name",
  "HomePhone": "$profile_home_phone",
  "MobilePhone": "$profile_mobile",
  "MailingCountry": "$profile_address_country",
  "MailingCity": "$profile_address_locality",
  "MailingState": "$profile_address_administrative_area_level_1",
  "MailingPostalCode": "$profile_address_postal_code",
  "MailingStreet": "$profile_address_route",
  "Preferred_contact_method__c": "Email",
  "primaryGuardian": {
    "firstName": "$profile_guardian_first_name",
    "lastName": "$profile_guardian_last_name",
    "MailingCountry": "$profile_guardianaddress_country",
    "MailingCity": "$profile_guardianaddress_locality",
    "MailingState": "$profile_guardianaddress_administrative_area_level_1",
    "MailingPostalCode": "$profile_guardianaddress_postal_code",
    "MailingStreet": "$profile_guardianaddress_route",
    "MobilePhone": "$profile_guardian_mobile",
    "Email": "some@cse.com",
    "relationship": "Mother"
  }
}

I'm using the following code:

Contact goScholar = (Contact)JSON.deserialize(request, Contact.class);

//get primaryGuardian
Map<String, Object> body = (Map<String, Object>)JSON.deserializeUntyped(request);
String primaryGuardianJson = body.get('primaryGuardian').toString();

// Getting the error at the below line
Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(primaryGuardianJson);
//get relationship for a junction record field
String relationship = data.get('Relationship').toString();

Contact primaryGuardian = (Contact)JSON.deserialize(primaryGuardianJson, Contact.class);

I'm getting the following error:

System.JSONException: Unexpected character ('E' (code 69)): was expecting double-quote to start field name at [line:1, column:3]

Class.System.JSON.deserializeUntyped: line 11, column 1
Class.ContactService.createPrimaryGuardian: line 78, column 1
Class.ContactService.updateContact: line 38, column 1
Class.ContactAPI.replace: line 35, column 1

I'm guessing that when using deserializeUntyped for the first time, it removes the " which is causing the error. Is there a solution for this?

0

I updated the above code to the following and it worked:

Contact goScholar = (Contact)JSON.deserialize(request, Contact.class);
//get primaryGuardian
Map<String, Object> body = (Map<String, Object>)JSON.deserializeUntyped(request);

//Start of Updated lines ---------------------
Map<String, Object> primaryGuardianMap = (Map<String, Object>)body.get('primaryGuardian');
String primaryGuardianJson = JSON.serialize(primaryGuardianMap);
//End of Updated lines ---------------------


Map<String, Object> data = (Map<String, Object>)JSON.deserializeUntyped(primaryGuardianJson);
String relationship = data.get('Relationship').toString();

Contact primaryGuardian = (Contact)JSON.deserialize(primaryGuardianJson, Contact.class);
| improve this answer | |
  • Note that this will be incredibly slow and run into CPU errors pretty quickly if you ever intend to use this in a loop. – sfdcfox Apr 30 at 5:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.