0

I am getting response from json and its showing in debug log but when json is very large then nested loop is not working and cpu time limit exceed. how to use map instead of nested for loop.

      jsonobj = jsonparse.parse(response.getBody());

    system.debug('jsonobj Status :='+jsonobj.Status);
    system.debug('jsonobj Number :='+jsonobj.Number);


    for(integer i=0;i<jsonobj.items.size(); i++){
        system.debug('jsonobj.items.qty :='+jsonobj.items[i].qty);
        system.debug('jsonobj.items.sNumber :='+jsonobj.items[i].sNumber);

          for(integer j=0;j<jsonobj.items[i].snpScs.size(); j++ ){
                system.debug('jsonobj.items.snpScs.margin:='+jsonobj.items[i].snpScs[j].margin);

                 for (integer k=0; k<jsonobj.items[i].Scs[j].optionSkus.size(); k++){
                    system.debug('jsonobj.items.Scs.optionSkus.lP := '+jsonobj.items[i].Scs[j].optionSkus[k].lP);
                 }
         }
    }
  • 1
    What's the format of your JSON? What's the relationship of snpScs and Scs? From what I can tell, you're basically iterating over all Scs items per each snpScs item, causing a multiplicative effect. A map isn't going to help you here, you're going to want to find a way to optimize away the multiplicative effect. – sfdcfox Jan 10 '18 at 7:27
1

You should use variables to assign each variable and and run loops based on that and access inner loops through local variables. What you have done is accessing the whole variable everytime and causing slow performance

Example illustration code modified from your code.There could be type mismatches since i dont know the types of variables from your response

 jsonobj = jsonparse.parse(response.getBody());

system.debug('jsonobj Status :='+jsonobj.Status);
system.debug('jsonobj Number :='+jsonobj.Number);

integer ijsonsize = jsonobj.items.size();//assigning local variable
for(integer i=0;i<ijsonsize; i++){
    items__C sItems = sonobj.items[i];//assigning local variable
    system.debug('jsonobj.items.qty :='+sItems.qty);
    system.debug('jsonobj.items.sNumber :='+sItems.sNumber);
    integer ispecssize = sItems.snpScs.size();//assigning local variable
      for(integer j=0;j< ispecssize; j++ ){
          snpscs sSnps = sItems.snpScs[j];//assigning local variable
            system.debug('jsonobj.items.snpScs.margin:='+sSnps.margin);
        integer soptionsize = sSnps.optionSkus.size();//assigning local variable
             for (integer k=0; k<soptionsize; k++){
                String optionssks = sSnps.optionSkus[k].lP;//assigning local variable
                system.debug('jsonobj.items.Scs.optionSkus.lP := '+optionssks);
             }
     }
}
|improve this answer|||||
1

For code clarity (and to reduce the risk of mixing up the i, j and k usages) and perhaps a minor further improvement in performance compared to RedDevil's answer, I suggest this for-each type of for loop:

for (ItemType item : sonobj.items) {
    for (SnpScType snpSc = item.snpScs) {
        for (OptionSkuType optionSku : snpSc.optionSkus) {
             String optionssks = optionSku.lP;
             system.debug('jsonobj.items.Scs.optionSkus.lP := '+optionssks);
        }
    }
}

But with nested loops CPU grows exponentially, so finding a way to not enter the child loops i.e. can you filter e.g. if (item.Type__c == 'abc') for most cases is probably going to be necessary of the JSON data is large.

|improve this answer|||||

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.