Following on from Any Ideas for Building dynamic JSON with apex. Trying to build json literal from an sObject dynamically (according to custom metadata - see here for more detais) I have allowed Json_field__c to be a path as @PhilW said e.g. item.items.item.extetnalId. for the externalId in the following Json output:

    "entity": { "externalId": "{{customerExternalId}}" },
    "item": {
        "items": [{
            "item": { "externalId": "{{itemExternalId}}" },
            "rate": 60

In first the code builds the new value from end to start in a regressive list of Map<String,Object> e.g.
rightSideMapsList[0] = {externalId = value}
rightSideMapsList[1] = {item = {externalId = value}}

(for array including it in a List<Map<String,Object>> then turning it to be an object in next Map<String,Object>).

In second i check if the first key(most outer "item") exists (if not found i add rightSideMapsList[rightSideMapsList.size()-1] to theresultMapMap<String,Object>) if does exists i remove that key from result Map and iterating up until by casting objects to Map<String,Object> until Reaching non existing key preserving values in a List of map<string,object>.

my problem was what do i do with an array of objects how can i identify to which object i should add the new key value pair? e.g. if i had another pair of { "item : {}}, "rate" : 90 } and i need to add "stage : "close won"..


You must log in to answer this question.

Browse other questions tagged .