You need to sign in to do that
Don't have an account?
![Shweta Garg Shweta Garg](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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.
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.
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.
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
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
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
Please mark correct answer & close this chain if you have your solution. This will help other community memebers.
Thanks