I need to build a json for external rest service(non salesforce platform) dynamically according to mapping stored in a custom metadata object (named SobjectToJSONFieldsMapping__mdt) records(i.e. including custom fields as :sobject_field__c, Json_field__c, sobject_type__c, XXX_type__c(external platform), etc.). i have done it before with JSON object having only one level of key-value pairs, in this case i have more for example:
{ "entity": { "externalId": "{{customerExternalId}}" }, "item": { "items": [{ "item": { "externalId": "{{itemExternalId}}" }, "rate": 60 }] } } it has to be totaly dynamic(not hard coded). In the example above I would have a record that has
record1:
sobject_field__c = 'Opportunity', Json_field__c = 'item'(outer one), record2:
sobject_field__c = 'AccountId', Json_field__c = 'externalId' In order to distinguish between the outer 'item' and 'inner', I have added another custom field in SobjectToJSONFieldsMapping__mdt named parents_scheme__c which contains a string of comma separated parents(from inner to outer in order to treat them with apex and build JSON respectively) e.g. for inner 'item' key (Json_field__c = 'item') parents_scheme__c would have 'items,item'.
Anyone have a better idea?