I am relatively new to Apex, but as a Java programmer the language does not feel alien. While doing a few trailheads to get me up to speed, I came across the Apex Rest Callouts module. While I was able to pass the challenge relying on the code snippet provided (here is my initial code), I was not convinced that it is the best way to parse a JSON response. And so began my search. Sharing my experience, just in case somebody finds it useful.
I first tried the developer reference, but totally struggled to make it work for the JSON I was working with.
My second port of call was JSON2Apex. And the answer was promising here, though it did take me some time to get to it. I would recommend the following steps for hassle free JSON parsing:
Step 1: Go to JSON2Apex application, paste your JSON, name your class and check the "Create explicit parse code" option. I don't do that initially and wasted a lot of time! You will get a zip file with a parser class and a test class. For the purpose of this post we will focus on the parser class. My parser class from JSON2Apex looked as linked.
Step 2: If you try to copy this into your developer console, the class will show an error as it has an inner class with the same name as the outer class!
Step 3: Use this parser to handle to JSON received from the callout. As you can see in the linked code, this is much more elegant and easier to code with, with all the heavy lifting done by the parser created by SFDC.
While this is done for a simple example, I am fairly confident that this can be done for complex JSON as well. Hopefully this helps you to simplify your JSON parsing and make your integration less painful!
I first tried the developer reference, but totally struggled to make it work for the JSON I was working with.
My second port of call was JSON2Apex. And the answer was promising here, though it did take me some time to get to it. I would recommend the following steps for hassle free JSON parsing:
Step 1: Go to JSON2Apex application, paste your JSON, name your class and check the "Create explicit parse code" option. I don't do that initially and wasted a lot of time! You will get a zip file with a parser class and a test class. For the purpose of this post we will focus on the parser class. My parser class from JSON2Apex looked as linked.
Step 2: If you try to copy this into your developer console, the class will show an error as it has an inner class with the same name as the outer class!
//not allowed in Apex! public class AnimalParse { //move the code inside inner class here public class AnimalParse { //get rid of inner class //after moving the code //some code, to move up } }Just take the content inside the inner class with the same name and move it up so that it becomes part of the top most class. Most probably it will be some member classes represented in JSON and a constructor for the outer class (as was in my case). For reference, linking my changed AnimalParser file.
Step 3: Use this parser to handle to JSON received from the callout. As you can see in the linked code, this is much more elegant and easier to code with, with all the heavy lifting done by the parser created by SFDC.
While this is done for a simple example, I am fairly confident that this can be done for complex JSON as well. Hopefully this helps you to simplify your JSON parsing and make your integration less painful!
Salesforce CRM tool helps business to track customer information of your business and the tool provides the detailed information of customer activities related to your business….You provide us detailed information on the complete guidance of Salesforce CRM tool.
ReplyDeleteRegards:
Salesforce Training in Chennai
Salesforce Training