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
Sai ThejaSai Theja 

Help me in JSON generator to check record exits

Json Body:

"clients_services": [
            {
                "name": "FEZ",
                "active": true,
                "billing_rate": "11.0",
                "billing_rate_type": "of_fee"
            },
            {
                "name": "State",
                "active": true,
                "billing_rate": "11.0",
                "billing_rate_type": "of_fee"
            },
            {
                "name": "WOTC",
                "active": false,
                "billing_rate": "11.0"
            }
        ]

apex class:

List<Map<String, Object>> myMaps = new List<Map<String, Object>>();  
    List<Object> cs_results = (List<Object>) sf_result.get('clients_services');     
    list<Clients_Services__c> cs = [select Active__c, Active_Checkbox__c, service__c, Billing_Rate__c, Billing_Rate_Type__c,opportunity__c FROM Clients_Services__c where Opportunity__r.AccountId = :id];
    for (Object res : cs_results) {     
    }      


Here i need to check if record exits update else insert. 
Ajay K DubediAjay K Dubedi
Hi Sai,
Follow this code:
public class testJsonUpsert {
    public static void testjson() {
        string sf_result = '{ "clients_services": [{ "name": "FEZ", "active": true, "billing_rate": "11.0", "billing_rate_type": "of_fee" }, { "name": "State", "active": true, "billing_rate": "11.0", "billing_rate_type": "of_fee" }, { "name": "WOTC", "active": false, "billing_rate": "11.0" } ] }';
        
        Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(sf_result);
        List<clients_services__c> csList = new List<clients_services__c>();
        List<Object> cs_results = new List<Object>();
        
        if(meta.containsKey('clients_services')) {
            cs_results = (List<Object>) meta.get('clients_services');
        }
        
        if(cs_results.size() > 0) {
            for(Object o : cs_results) {
                Map<String, Object> metaVal = (Map<String, Object>)o;
                clients_services__c csObj = new clients_services__c();
                if(metaVal.containsKey('name')) {
                    csObj.Name = string.valueof(metaVal.get('name'));
                }
                if(metaVal.containsKey('active') == true) {
                    if(metaVal.get('active')) {
                        csObj.Active__c = true;
                    } else {
                        csObj.Active__c = false;
                    }
                    
                }
                if(metaVal.containsKey('billing_rate')) {
                    csObj.billing_rate__c = string.valueof(metaVal.get('billing_rate'));
                }
                
                if(metaVal.containsKey('billing_rate_type')) {
                    csObj.billing_rate_type__c = string.valueof(metaVal.get('billing_rate_type'));
                }
                //Do the same thing for other fields.
                csList.add(csObj);
            } 
        }
        insert csList;
    }
}

And if you want to upsert record (insert or update), you need to make a custome field as external id and then using this field you can upsert record.
Follow this link for upsert:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_upsert.htm

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