You need to sign in to do that
Don't have an account?
Ahetesham Mansuri 3
Need to Create a JSON in the Specified Format
I am trying to create a JSON in the specified format below but i am not able to create the exact JSON format. i am using an apex class here to create the JSON format.
JSON Format --
JSON Format --
{ "records": [ { "attributes": { "type": "Product2", "referenceId": "pref1" }, "name": "Product 2", "SBQQ__Features__r": { "records": [ { "attributes": { "type": "SBQQ__ProductFeature__c", "referenceId": "ref1" }, "Name": "Feature1", "SBQQ__Number__c": 1, "SBQQ__MinOptionCount__c": 1 } ] } }, { "attributes": { "type": "Product2", "referenceId": "pref2" }, "name": "Product 2", "SBQQ__Features__r": { "records": [ { "attributes": { "type": "SBQQ__ProductFeature__c", "referenceId": "ref2" }, "Name": "Feature1", "SBQQ__Number__c": 1, "SBQQ__MinOptionCount__c": 1 } ] } } ] }Apex Class -
public class JSonCLassForParent_Child { public List<Records_Z> records; public class SBQQ_Features_r { public List<Records> records; } public class Records_Z { public Attributes attributes; public String name; public SBQQ_Features_r SBQQ_Features_r; } public class Attributes { public String type; public String referenceId; public Attributes(String type,String referenceId){ this.type = type; this.referenceId = referenceId; } } public class Records { public Attributes attributes; public String Name; public Decimal SBQQ_Number_c; public Decimal SBQQ_MinOptionCount_c; } public static String createJSONforParent_Child(List <Id> pIds){ List<Product2> pList = [Select Id,Name from Product2 where Id = :pIds]; System.debug(pList); List<Records> rec = new List<Records>(); List<Records_Z> rz = new List<Records_Z>(); Map<Id,Product2> aMap = new Map<Id,Product2>([Select Id,Name from Product2 where Id = :pIds]); System.debug('map is ' +aMap); List<SBQQ__ProductFeature__c> fList = [Select Id,Name,SBQQ__Number__c,SBQQ__MinOptionCount__c,SBQQ__ConfiguredSKU__c from SBQQ__ProductFeature__c where SBQQ__ConfiguredSKU__c In : aMap.keySet()]; System.debug('feature is'+fList); // Map<Id,SBQQ__ProductFeature__c> productFeatureMap = new Map<Id,SBQQ__ProductFeature__c>(); Integer count = 0; for(SBQQ__ProductFeature__c f : fList){ count++; if(aMap.containsKey(f.SBQQ__ConfiguredSKU__c)){ Records r = new Records (); r.attributes = new Attributes('SBQQ_ProductFeature_c','ref'+count); r.Name = f.Name; r.SBQQ_Number_c = f.SBQQ__Number__c; r.SBQQ_MinOptionCount_c = f.SBQQ__MinOptionCount__c; rec.add(r); // productFeatureMap.put(f.SBQQ__ConfiguredSKU__c,f); } } SBQQ_Features_r sbf = new SBQQ_Features_r(); sbf.records = rec; Integer count2 = 0; for(Product2 a : pList){ count2++; Records_Z r = new Records_Z(); r.attributes = new Attributes('Product2','pref'+count2); r.name = a.Name; r.SBQQ_Features_r = sbf; //r.SBQQ_Features_r = productFeatureMap.get(a.Id); rz.add(r); } System.debug(rz); Map <String,Object> parentMap = new Map <String,Object> (); parentMap.put('records',rz); System.debug(JSON.serialize(parentMap)); String jsonString = JSON.serialize(parentMap); String finalJsonString = jsonString.replaceAll('_','__'); System.debug(finalJsonString); return finalJsonString; } }
Please find below JSON. This might solve your issue.
If this helps, Please mark it as best answer.
Thanks!