Integrating external systems with Salesforce is a common requirement, and often those systems communicate using JSON or XML. While Apex provides native tools to parse these formats, developers quickly run into limitations, especially as payload complexity grows.
In this blog, we’ll explore:
- How to parse JSON and XML in Apex (with examples)
- The challenges of traditional parsing approaches
- Why modern transformation tools like DataWeave are becoming essential

Parsing JSON in Apex
Apex provides built-in classes like JSON.deserialize() and JSONParser for handling JSON.
Example JSON
{
"name": "Mahesh Raja",
"age": 30,
"isActive": true
}
Approach 1: Using a Wrapper Class (Recommended for structured data)
public class Customer {
public String name;
public Integer age;
public Boolean isActive;
}
String jsonString = '{"name":"Mahesh Raja","age":30,"isActive":true}';
Customer cust = (Customer) JSON.deserialize(jsonString, Customer.class);
System.debug(cust.name); // Mahesh Raja
Approach 2: Using Untyped Parsing (Dynamic JSON)
String jsonString = '{"name":"Mahesh Raja","age":30}';
Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
System.debug(result.get('name'));
Parsing XML in Apex
XML parsing is handled via the Dom.Document class.
Example XML
<customer>
<name>John Doe</name>
<age>30</age>
</customer>
Example Apex Code
String xmlString = '<customer><name>John Doe</name><age>30</age></customer>';
Dom.Document doc = new Dom.Document();
doc.load(xmlString);
Dom.XmlNode root = doc.getRootElement();
String name = root.getChildElement('name', null).getText();
String age = root.getChildElement('age', null).getText();
System.debug(name);
Challenges with Traditional Apex Parsing
While the above methods work well for simple payloads, real-world integrations introduce several complications:
1. Deeply Nested Structures
JSON and XML payloads can be highly nested.
- Requires multiple wrapper classes
- Leads to verbose and hard-to-maintain code
- Increased risk of null pointer exceptions
2. Schema Changes Break Code
External APIs evolve frequently.
- Adding/removing fields breaks strongly typed deserialization
- Requires frequent code updates and deployments
3. Dynamic and Inconsistent Data
APIs may return:
- Optional fields
- Different structures for different scenarios
- Arrays vs objects inconsistently
Handling this in Apex:
if(result.containsKey('data')) {
// defensive coding everywhere
}
4. XML Parsing is Verbose and Error-Prone
- Manual traversal of nodes
- Namespace handling complexity
- No direct mapping to classes (unlike JSON)
5. Transformation Logic is Hard to Maintain
Often, parsing is just step one. You also need to:
- Transform data structure
- Map fields between systems
- Apply business logic
This leads to:
- Mixed concerns (parsing + transformation + logic)
- Difficult debugging
- Poor readability
6. Performance and Governor Limits
- Large payloads increase heap size
- Recursive parsing can hit CPU limits
- Inefficient loops and maps increase execution time
The Real Problem: Apex is Not a Transformation Language
Apex is designed for business logic—not for complex data transformation.
When used for parsing + mapping + transformation:
- Code becomes rigid
- Maintenance cost increases
- Integration agility decreases
Conclusion
Parsing JSON and XML in Apex works well for simple and predictable integrations. However, as payloads become more complex, dynamic, and deeply nested, the limitations of traditional approaches become increasingly evident. What starts as straightforward parsing can quickly turn into verbose, tightly coupled, and hard-to-maintain code.
The core challenge lies in using a language designed for business logic to handle heavy data transformation. This leads to mixed concerns, reduced readability, and slower adaptability when external systems evolve.
To build scalable, resilient, and future-ready integrations, it’s important to rethink how we approach data transformation in Salesforce.
In our next blog, we’ll introduce a powerful new concept – DataWeave – and explore how it simplifies complex transformations while bringing clarity, flexibility, and efficiency to your integration design.

