If you are building an integration in Salesforce, you will inevitably have to handle JSON data. Manually parsing through complex, nested JSON strings is tedious and error-prone. By generating Apex classes that perfectly mirror the JSON structure, you can deserialize the data in a single line of code.
In this tutorial, we will walk through the exact steps to convert a raw JSON response into strongly-typed Apex classes and output the data.
Prerequisites
- Basic understanding of Salesforce development and Apex syntax.
- Access to a Salesforce Developer Edition or Sandbox environment.
Step 1: The Raw JSON Response
Before we create our Apex classes, let's look at the data we are receiving. Imagine we are calling an external HR system API that returns a list of employees in the following JSON format:
{
"employees": [
{
"name": "John Doe",
"age": 30,
"designation": "Software Engineer"
},
{
"name": "Jane Smith",
"age": 28,
"designation": "Technical Writer"
}
]
}
Step 2: Generate the Apex Wrapper Classes
To translate this JSON, we need to create Apex classes that map exactly to this structure. While you can use the famous JSON2Apex.herokuapp.com generator tool to automate this for massive payloads, our example is simple enough to write manually.
1. Click the Gear Icon and open the Developer Console.
2. Go to File > New > Apex Class.
3. Name the class
EmployeeWrapper.4. Paste the code below and hit Save.
public class EmployeeWrapper {
// The main list that matches the "employees" array in the JSON
public List<Employee> employees;
// The inner class defining a single employee's attributes
public class Employee {
public String name;
public Integer age;
public String designation;
}
}
Notice that we used the
public access modifier for name, age, and designation. If you forget to make your wrapper variables public, Salesforce's JSON parser won't be able to access them, and your variables will silently return null!
Step 3: Deserializing the JSON
Now comes the magic. We will use the built-in JSON.deserialize() method to inject our raw JSON string into the wrapper class we just built.
Open the Execute Anonymous Window in your Developer Console (Ctrl+E or Cmd+E) and run this code:
// 1. Mocking the JSON response we received from the API
String jsonStr = '{"employees":[{"name":"John Doe","age":30,"designation":"Software Engineer"},{"name":"Jane Smith","age":28,"designation":"Technical Writer"}]}';
// 2. The Magic Line: Converting the string into an Apex Object
EmployeeWrapper empWrapper = (EmployeeWrapper) JSON.deserialize(jsonStr, EmployeeWrapper.class);
// 3. Testing our new structured data
System.debug('Employee 1 Name: ' + empWrapper.employees[0].name);
System.debug('Employee 1 Role: ' + empWrapper.employees[0].designation);
System.debug('Employee 2 Name: ' + empWrapper.employees[1].name);
Step 4: Viewing the Output
Once you execute the code, click the Logs tab at the bottom of the Developer Console. Check the Debug Only box to filter out the noise. You will see your perfectly parsed data:
14:02:18:004 USER_DEBUG [8]|DEBUG|Employee 1 Name: John Doe 14:02:18:004 USER_DEBUG [9]|DEBUG|Employee 1 Role: Software Engineer 14:02:18:004 USER_DEBUG [10]|DEBUG|Employee 2 Name: Jane Smith
1. Capture: Receive the raw JSON payload from the HTTP Callout.
2. Structure: Pass the sample JSON through a JSON2Apex generator to build your Wrapper Classes.
3. Deserialize: Use
JSON.deserialize(payload, Wrapper.class) to convert the text.4. Process: Loop through your new strongly-typed lists and update Salesforce records as needed.
Conclusion
Generating Apex classes based on JSON structures completely eliminates the headache of manual string parsing. By using wrapper classes alongside Salesforce's native JSON.deserialize() method, you can handle massive payloads from external APIs cleanly and efficiently. Whether you write the classes by hand or use an automated JSON2Apex tool, mastering this pattern is a must for any integration developer.
Happy coding!