[SalesForce] Deserialize hits memory limit

We have a custom apex webservice in which we do certain operations and later call a managed package apex method that returns a JSON string which we deserialize and send back to the client. For example purposes let's assume the following JSON structure is returned from the managed package method:

{
    "x": "xxx",
    "y": "yyy",
    "z": "zzz",
    "lines": [{
        "a": "aaa",
        "b": "bbb",
        "c": "ccc",
        "record": {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v44.0/sobjects/Account/001000000000000000"
            },
            "Id": "001000000000000000",
            "AccountNumber": "12345",
            "Contacts": {
                "totalSize": 1,
                "done": true,
                "records": [{
                    "attributes": {
                        "type": "Contact",
                        "url": "/services/data/v44.0/sobjects/Contact/003S00000000000000"
                    },
                    "Id": "003S00000000000000",
                    "Phone": "22323423423",
                    "MobilePhone": "345345345345"
                }]
            }
        }
    }]
}

Taking the above example we could assume that we could deserialize the above JSON string into the following classes:

public class MainModel {

    String x;
    String y;
    String z;
    LinesModel[] lines;
}


public class LineModel {

    String a;
    String b;
    String c;
    Account record;
}

As we can see the LineModel states that the record attribute has an Account sobject type, therefore it can span children relationships of Contacts, Opportunities and many other children relationships.

Therefore we could deserialize the JSON the following way:

MainModel model = (MainModel)JSON.deserialize(jsonString, MainModel.class);

Before returning the model to our webservice caller we want to get rid of some information that the manage package includes but we don't want, for example the Contacts children tree for each Account.

In order to remove it, we are doing the following:

Map<String, Object> genericModel = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
Object[] lines = (Object[])genericModel.get('lines');

Map<String, Object> itemObj;
Map<String, Object> record;

for (Object item : lines) {

    itemObj = (Map<String, Object>)item;
    record = (Map<String, Object>)itemObj.get('record');

    if (record.containsKey('Contacts')) {
        record.remove('Contacts')
    }
}

MainModel model = (MainModel)JSON.deserialize(JSON.serialize(genericModel), MainModel.class);

The issue we're facing is that when the response from the manage package has many nested records the desarialization process (the deserializeUntyped call) fails with an Apex heap memory limit. When I say nested records, in the context of the above example there could maybe be only 20 Accounts but each account maybe having 60 contacts.

Since we don't have control on what the manage package returns in the JSON we cannot avoid having those unnecessary children.

We've tried removing the children using regex directly on the JSON string the following way to avoid unnecessary deserialization for cleaning:

String textPattern = '\\"Contacts\\":.*records.*\\[.*]},';

String resultText = jsonString.replaceAll(textPattern, '');

MainModel model = (MainModel)JSON.deserialize(JSON.serialize(resultText), MainModel.class);

But this results on a System.LimitException: Regex too complicated error because the JSON string is too large.

Any ideas on how we can clean the unwanted JSON data before returning it to the caller that could be viable?

Please take into account that the above is an example very simplified for the sake of understanding the real issue, the real JSON has many other relationships and objects that need cleaning and is more complex but the scenario is pretty much the same.

Best Answer

In your wrapper class LineModel, don't use Standard Account. If you do that you will also deserialize children. Instead, create Apex Wrapper for Account with fields you need. This will be more memory efficient.

public class LineModel {

        String a;
        String b;
        String c;
        MyAccount record;
    }

    public Class MyAccount{
        String Id;
        String AccountNumber;
    }

    public class MainModel {

        String x;
        String y;
        String z;
        List<LineModel> lines;
    }
Related Topic