I have a JSON too large to be deserialize, and I'm getting the Exception System.LimitException: Apex heap size too large: 6127921.

I used groupeRame = (groupeRame) JSON.deserialize(jsonRequest, groupeRame.class), where groupeRame.class is a inner class wrapper.

I tried JSON2Apex but it did not work too.

Would anyone have an idea to correct this problem?

Thanks in advance.

  • defer the work to a queueable (limit will be 12MB and 60sec CPU) – cropredy Jul 23 '19 at 23:28
  • Thanks how to process ? – Sylvain Coppin Jul 24 '19 at 11:14

Transaction receiving the JSON (CPU limit = 10 sec, Heap = 6MB)

for example, an APEX REST service that has a method annotated @HttpPost

System.enqueueJob(new MyQueueable(jsonRequest));

Queueable outer (or inner) class (CPU limit = 60 sec, Heap = 12MB)

public class MyQueueable implements System.Queueable {

  String jsonBody;
  public MyQueueable(String jsonBody) {this.jsonBody = jsonBody;}

  public execute(QueueableContext qc) {

    MyApexType myType = (MyApexType) Json.deserialize(this.jsonBody,MyApexType.class);
    ... do something with deserialized myType
| improve this answer | |
  • Can use it in @HttpPost webservice static Retour importGestionPrioriteClient() { ? – Sylvain Coppin Jul 31 '19 at 15:05
  • @SylvainCoppin - edited my answer – cropredy Jul 31 '19 at 17:02
  • It's work but i can't have the return of the request, because execute is asynchronous. – Sylvain Coppin Aug 1 '19 at 13:41
  • Well, you need to either get the poster to reduce the size of the Json or change the interface such that the reply is async. The SFDC limit is a hard limit. – cropredy Aug 1 '19 at 13:59

I dont think its possible with apex as synchronous limit is 6mb. Instead can you try returning to front end (Lightning Aura component/LWC or page) and process it by using JSON.parse(jsonRequest)

However, if this is used just for database operations and not showing anything on UI, then put this logic in future or queueable methods. Async apex limits for heap is 12mb.

| improve this answer | |
  • It is a webservice and I try to deserialyse the request body response : String jsonRequest = RestContext.request.requestBody.toString(); groupeRame = (groupeRame) JSON.deserialize(jsonRequest, groupeRame.class); – Sylvain Coppin Jul 23 '19 at 14:06
  • are you saving anything to database in salesforce OR are you displaying in UI? – salesforce-sas Jul 23 '19 at 14:09
  • After trying to deserialyze , yes I insert the data into an sObject – Sylvain Coppin Jul 23 '19 at 14:12
  • And for information when I used JSON2Apex I have a CPU limit exceeded Exception – Sylvain Coppin Jul 23 '19 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.