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
Sem IngrafSem Ingraf 

HELP JSON Formatting

hello everyone, 

I need some help to be able to get the infomrations of this small part of JSON : 

 

"cancellation_policies":{
   "alidays":[
      {
         "text_message":"Penale voli emessi o non emessi: 200 euro a persona",
         "penalty":{
            "money":{
               "currency":"EUR",
               "amount":500
            }
         }
      }
   ],
   "agency":[
      {
         "text_message":"Penale voli emessi o non emessi: 200 euro a persona",
         "penalty":{
            "money":{
               "currency":"EUR",
               "amount":875.43
            }
         }
      }
   ]
}




I say small part since the passed JSON turns out to be much larger, but I am having difficulty with getting the data from this part only. 

I created my wrapper class to help me with the deserialization after I wrote this to be able to get the "cancellation_policies" part 

 

 

public Map<String, Object> serviceInfo;
this.serviceInfo = (Map<String, Object>) deserializedObj.get('cancellation_policies');
        system.debug('cancellation_policies' +serviceInfo);


Now, however, i cannot get the information that is within my two lists : alidays and agency, 

I should save the information to custom fields such as : 


CancellationPolicies__c = alidays.text_message;
CancellationPoliciesPublic__c =agencies.text_message;
AlidayPolicieAmount__c alidays.penalty.money.amount;
etc.
 

Thanks for help. 

Julien SalensonJulien Salenson
Hi sem,
 
Here's a sample Apex class that does that:
public class CancellationPolicyParser {
    public String CancellationPolicies__c { get; set; }
    public String CancellationPoliciesPublic__c { get; set; }
    public Decimal AlidayPolicieAmount__c { get; set; }

    // Constructor to parse the JSON and populate the fields
    public CancellationPolicyParser(String jsonInput) {
        Map<String, Object> jsonData = (Map<String, Object>) JSON.deserializeUntyped(jsonInput);

        // Get the "alidays" cancellation policy
        List<Object> alidaysPolicies = (List<Object>) jsonData.get('alidays');
        if (alidaysPolicies != null && alidaysPolicies.size() > 0) {
            Map<String, Object> alidaysPolicy = (Map<String, Object>) alidaysPolicies[0];
            CancellationPolicies__c = (String) alidaysPolicy.get('text_message');
            Map<String, Object> penalty = (Map<String, Object>) alidaysPolicy.get('penalty');
            if (penalty != null) {
                Map<String, Object> money = (Map<String, Object>) penalty.get('money');
                if (money != null) {
                    AlidayPolicieAmount__c = (Decimal) money.get('amount');
                }
            }
        }

        // Get the "agency" cancellation policy
        List<Object> agencyPolicies = (List<Object>) jsonData.get('agency');
        if (agencyPolicies != null && agencyPolicies.size() > 0) {
            Map<String, Object> agencyPolicy = (Map<String, Object>) agencyPolicies[0];
            CancellationPoliciesPublic__c = (String) agencyPolicy.get('text_message');
        }
    }
}
You can then use this class to parse the JSON and populate the custom fields in your Salesforce records:
String jsonInput = '...'; // Replace with your JSON input
CancellationPolicyParser parser = new CancellationPolicyParser(jsonInput);

// Populate custom fields in your Salesforce record
YourObject__c record = new YourObject__c();
record.CancellationPolicies__c = parser.CancellationPolicies__c;
record.CancellationPoliciesPublic__c = parser.CancellationPoliciesPublic__c;
record.AlidayPolicieAmount__c = parser.AlidayPolicieAmount__c;

// Insert or update the record as needed
insert record; // Or update if it's an existing record

Make sure to replace YourObject__c with the actual API name of your Salesforce custom object where you want to store this information.

I hope this helps
Julien