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
Shweta GargShweta Garg 

Fastest way to parse JSON in apex

Hello,
I need to parse JSON content. i think there are three ways to parse JSON.
1. Using deserialize method
2. JSON Parser is another solution.
3. using split method of String..

I want to use Deserialize method because it is very easy to maintain and fast too.
This is my JSON content that I want to parse.
{"size" : 391,"totalSize" : 391,"done" : true,"queryLocator" : null,"entityTypeName" : "Layout","records" : [ {"attributes" : {"type" : "Layout","url" : "/services/data/v34.0/tooling/sobjects/Layout/00h28000006RG0VAAW" },"FullName" : "Account-Account %28Marketing%29 Layout","Metadata" : {"customButtons" : null,"customConsoleComponents" : null,"emailDefault" : false,"excludeButtons" : null,"feedLayout" : null, "headers" : [ "PersonalTagging", "PublicTagging" ],"layoutSections" : [ {"customLabel" : false,"detailHeading" : false,"editHeading" : true,"label" : "Account Information","layoutColumns" : [ { "layoutItems" : [ {"analyticsCloudComponent" : null,"behavior" : "Edit","canvas" : null,"component" : null, "customLink" : null,"emptySpace" : null,"field" : "OwnerId","height" : null,"page" : null,"reportChartComponent" : null,"scontrol" : null,"showLabel" : null,"showScrollbars" : null,"width" : null} ],"reserved" : null}], "style" : "TwoColumnsTopToBottom"}],"relatedLists" : [ {"customButtons" : [ ],"excludeButtons" : [ ],"fields" : [ "FULL_NAME", "CONTACT.TITLE", "CONTACT.EMAIL", "CONTACT.PHONE1" ],"relatedList" : "RelatedContactList","sortField" : null,"sortOrder" : null}], "relatedObjects" : [ "ParentId" ],"runAssignmentRulesDefault" : false,"showEmailCheckbox" : true,"showHighlightsPanel" : false,"showInteractionLogPanel" : false,"showKnowledgeComponent" : null,"showRunAssignmentRulesCheckbox" : true,"showSolutionSection" : null,"showSubmitAndAttachButton" : false,"summaryLayout" : null,"urls" : null,"quickActionList" : {"quickActionListItems" : [ {"quickActionName" : "FeedItem.QuestionPost"}]},"relatedContent" : {"relatedContentItems" : [ {"layoutItem" : {"analyticsCloudComponent" : null,"behavior" : null,"canvas" : null,"component" : "runtime_sales_social:socialPanel","customLink" : null,"emptySpace" : null,"field" : null,"height" : null,"page" : null,"reportChartComponent": null,"scontrol" : null,"showLabel" : null,"showScrollbars" : null,"width" : null}} ]}, "miniLayout" : null,"multilineLayoutFields" : null,"platformActionList" : null}}]}'

I need to find fields from JSON content.  field is inside Layout sections.I have to go  three levels deep for reaching exactly at field.
If I adopt that  first Way i.e, Desrialize , I will stuck in for loops. I will had to use for loop 4 times(for inside for).
JSON Parser is pretty tedious way to use. should i use JSON PArsermethods?Will It make processing slow.
and Splitting the Json String does not seem to good way to implement.

Please suggest me. Is thery any workaround to escape from 4 for loops while using desilization method.If No, What way should I use?
Please help me . I am confused.

Thanks and Regards.


 
Karan Shekhar KaulKaran Shekhar Kaul

Hi Shweta,

Try https://json2apex.herokuapp.com. Check "Create explicit parse code" checkbox & then create "Create Apex" button. This will downaload apex classes with already implemented parser. Hope this helps.

Regards,

Karan

Shweta GargShweta Garg
Hi Karan,

Thanks for your reply. I have checked the Herokuapp . I have implemented the parsing using desrialization and it is working fine. But the issue is ,As I said , I have to search for a field which is 3 level deep. So i had to use 3 for loops for each record. Here comes multilevel nesting.
 If I adopt JSON Parser. I think parser also tracks the entire JSON ony by one. IT must be slow too.
 Which way should I use. Is Deserialization fine? Or should I implement this using  JSON Parse?

Regards,
Shweta
Karan Shekhar KaulKaran Shekhar Kaul

Hi Shweta,

if your JSON structure does not change frequently then go for Deserialization . It is more clear & maintainable way to implement JSON parsing. I have been using this technique for a long time now. I just love it :) There are some scenarios where JSON being parsed has almost same structure(with small changes in 1 or 2 attributes) but different origination. In these type of scenarios we for JSON parser. 

Does that make sense?

Regards,
Karan

Karan Shekhar KaulKaran Shekhar Kaul
Hi Shweta,

Please mark correct answer & close this chain if you have your solution. This will help other community memebers. 
​Thanks