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
SazEastSazEast 

help with mapping JSON2Apex Class to Salesforce Object Fields

Hello, I'm a complete newbie to API integration - I have REST web service exposed to consume third party data which is to  update records in Salesforce. I've got the JSON2Apex Class as the JSON is nested but unsure how to use this to map JSON fields/objects to Salesforce field names. I'm struggling to find any guidance online, please could someone point me in the right direction?
VinayVinay (Salesforce Developers) 
Hi,

Hope below links gives you more information on JSON deserialization.

https://salesforce.stackexchange.com/questions/158493/field-mapping-from-json-response-to-custom-object
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm#apex_System_Json_deserializeUntyped
https://www.biswajeetsamal.com/blog/dynamic-data-creation-in-salesforce-from-json-data-without-using-wrapper-class/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
SazEastSazEast
Thanks Vinay, I think thats helped me to get a little further but i'm stuck withhow to refer to nested json. 

Can anyone please help me fix the below code?


exmaple JSON:
 
{
	"resourceType": "Human",
	"id": "1234567890",
	"text": {
"status": "generated",},
	"identifier": 
		{
			"use": "usual",
			"value": "1234567890"
		}
	,
	"active": true,
	"name": {
		"use": "usual",
		"family": "Smith",
		"given": 
			"John"
	},
	"mobile": {
		"system": "phone",
		"value": "077123456",
		"use": "mobile"
	}

Wrapper Class:
global with sharing class HumanDeserialize {    
    global List<Humans> lstHumanRecords;
    global HumanDeserialize(){lstHumanRecords = new List<Humans>();}
    global class Humans{
        global String resourceType;
        global String id;
        global List <text> text;  
    }
          global class text{
            global List <identifier> identifier;
   }
       global class identifier{
        	global String use;
        	global String value;
    }
    }

Rest upsert class:
 
@RestResource(urlMapping='/Humans__c/*')
global with sharing class HumanUpdateManager {
    @HttpPut 
    global static HumanDeserialize doPutHuman(HumanDeserialize requestHumanDeserialize){
        HumanDeserialize responseHumanDeserialize = new HumanDeserialize();
        List<HumanDeserialize.Humans> lsHumanDeserializes = new List<HumanDeserialize.Humans>();
        List<Human__c> lstHumanRecords = new List<Human__c>();
    
        for(HumanDeserialize.Humans Humandeserialize : requestHumanDeserialize.lstHumanRecords){
              
    		Human__c HumanRecord = new Human__c();
        	HumanRecord.ExtID__c = requestHumanDeserialize.identifier;
            lstHumanRecords.add(HumanRecord);           
        }        
        upsert lstHumanRecords;
             	responseHumanDeserialize = requestHumanDeserialize;
        responseHumanDeserialize.identifier = HumanRecord.ExtID__c;
        
        return  responseHumanDeserialize;
        
    }
}