function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel Peaper 8Daniel Peaper 8 

How To Loop Through Parsed JSON

Hi All, I'm looking for some advice on best practice. I want to loop through Options and I've come up with the following solution which actually works. What I want to know is whether or not my method of a loop within a loop is best practice or if there is a better way?

String json = '{'+
    '  \"TrackingCategories\": ['+
    '    {'+
    '      \"Name\": \"Region\",'+
    '      \"Status\": \"ACTIVE\",'+
    '      \"TrackingCategoryID\": \"351953c4-8127-4009-88c3-f9cd8c9cbe9f\",'+
    '      \"Options\": ['+
    '        {'+
    '          \"TrackingOptionID\": \"ce205173-7387-4651-9726-2cf4c5405ba2\",'+
    '          \"Name\": \"Eastside\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6eb12fdf-63de-4033-98df-be679d84e3c2\",'+
    '          \"Name\": \"North\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6159bdd4-b634-4338-a664-e929aa73f70f\",'+
    '          \"Name\": \"South\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"161ad543-97ab-4436-8213-e0d794b1ea90\",'+
    '          \"Name\": \"West Coast\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        }'+
    '      ]'+
    '    }'+
    '  ]'+
    '}';
TrackingOptionsResponse obj = TrackingOptionsResponse.parse(json);
System.debug('obj='+obj);



for(TrackingOptionsResponse.TrackingCategories r :obj.TrackingCategories){
    system.debug('r='+r.Name);
    System.debug('r2='+r.Options);
    for(TrackingOptionsResponse.Options l :r.options){
        System.debug('l='+l.Name);
    }
}

Thanks,

Dan

Best Answer chosen by Daniel Peaper 8
Maharajan CMaharajan C
Hi Daniel,

For the above data Loop inside loop is fine only... It's small amount of data... If it is a trigger you have to worry about loop inside loop...

And you can use the below way also for the above JSON instead of more looping:
 
String json = '{'+
    '  \"TrackingCategories\": ['+
    '    {'+
    '      \"Name\": \"Region\",'+
    '      \"Status\": \"ACTIVE\",'+
    '      \"TrackingCategoryID\": \"351953c4-8127-4009-88c3-f9cd8c9cbe9f\",'+
    '      \"Options\": ['+
    '        {'+
    '          \"TrackingOptionID\": \"ce205173-7387-4651-9726-2cf4c5405ba2\",'+
    '          \"Name\": \"Eastside\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6eb12fdf-63de-4033-98df-be679d84e3c2\",'+
    '          \"Name\": \"North\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6159bdd4-b634-4338-a664-e929aa73f70f\",'+
    '          \"Name\": \"South\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"161ad543-97ab-4436-8213-e0d794b1ea90\",'+
    '          \"Name\": \"West Coast\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        }'+
    '      ]'+
    '    }'+
    '  ]'+
    '}';


//system.debug(' --> ' +  json );

TrackingOptionsResponse obj = TrackingOptionsResponse.parse(json);
//System.debug('obj ==> '+obj);


system.debug( ' Name ==> ' + obj.TrackingCategories[0].Name );
for(TrackingOptionsResponse.Options r : obj.TrackingCategories[0].options){
        System.debug(' Optioon Name ==> '+ r.Name);
}


Thanks,
Maharajan.C

 

All Answers

umonub htztqgmcumonub htztqgmc
Noah Shannon Green is the eldest son of Hollywood star Megan Fox and Brian Austin Green. He is also famous as an American celebrity kid. Read more through this article her family life, sibling, parents, Age, Net worth & Life Facts.
Maharajan CMaharajan C
Hi Daniel,

For the above data Loop inside loop is fine only... It's small amount of data... If it is a trigger you have to worry about loop inside loop...

And you can use the below way also for the above JSON instead of more looping:
 
String json = '{'+
    '  \"TrackingCategories\": ['+
    '    {'+
    '      \"Name\": \"Region\",'+
    '      \"Status\": \"ACTIVE\",'+
    '      \"TrackingCategoryID\": \"351953c4-8127-4009-88c3-f9cd8c9cbe9f\",'+
    '      \"Options\": ['+
    '        {'+
    '          \"TrackingOptionID\": \"ce205173-7387-4651-9726-2cf4c5405ba2\",'+
    '          \"Name\": \"Eastside\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6eb12fdf-63de-4033-98df-be679d84e3c2\",'+
    '          \"Name\": \"North\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"6159bdd4-b634-4338-a664-e929aa73f70f\",'+
    '          \"Name\": \"South\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        },'+
    '        {'+
    '          \"TrackingOptionID\": \"161ad543-97ab-4436-8213-e0d794b1ea90\",'+
    '          \"Name\": \"West Coast\",'+
    '          \"Status\": \"ACTIVE\"'+
    '        }'+
    '      ]'+
    '    }'+
    '  ]'+
    '}';


//system.debug(' --> ' +  json );

TrackingOptionsResponse obj = TrackingOptionsResponse.parse(json);
//System.debug('obj ==> '+obj);


system.debug( ' Name ==> ' + obj.TrackingCategories[0].Name );
for(TrackingOptionsResponse.Options r : obj.TrackingCategories[0].options){
        System.debug(' Optioon Name ==> '+ r.Name);
}


Thanks,
Maharajan.C

 
This was selected as the best answer
Daniel Peaper 8Daniel Peaper 8
Thanks @Maharajan.C, that makes perfect sense.