Introduction
In Salesforce, working with JSON data is a common requirement when dealing with integrations, web services, or handling data from external sources. Salesforce provides several methods to parse JSON data into native Apex objects. One such method is json.deserializeUntyped, which allows developers to deserialize JSON data into an untyped Object. This gives you the flexibility to work with JSON data without defining specific classes.
In this blog, we'll explore the json.deserializeUntyped method in Salesforce, understand its usage, and provide a step-by-step example with full code and output.
What is json.deserializeUntyped?
The json.deseriadlizeUntyped method is part of Salesforce's Apex language, specifically designed to deserialize JSON data into an Object. This means that the result of the deserialization will be a generic Object instance, and you'll need to cast it to a specific type if you want to access its properties.
Example Scenario
Let's assume we have a JSON response from an external API that we want to parse and extract information from. The JSON response looks like this:
{
"name": "John Doe",
"age": 30,
"email": "john.doe@example.com",
"isSubscribed": true,
"preferences": {
"theme": "light",
"language": "en"
}
}
Our goal is to deserialize this JSON data into an untyped object and then access its properties to display the information.
Step-by-Step Example
- Create a new Apex class to house the code. Let's call it JsonParsingExample.
- Define the method to perform the deserialization and access the properties:
public class JsonParsingExample {
public static void parseJsonData(String jsonString) {
// Deserialize the JSON data into an untyped object
Object untypedObj = (Object) JSON.deserializeUntyped(jsonString);
// Access the properties of the untyped object
String name = (String) untypedObj.get('name');
Integer age = (Integer) untypedObj.get('age');
String email = (String) untypedObj.get('email');
Boolean isSubscribed = (Boolean) untypedObj.get('isSubscribed');
Map<String, Object> preferencesMap = (Map<String, Object>) untypedObj.get('preferences');
String theme = (String) preferencesMap.get('theme');
String language = (String) preferencesMap.get('language');
// Display the extracted information
System.debug('Name: ' + name);
System.debug('Age: ' + age);
System.debug('Email: ' + email);
System.debug('Is Subscribed: ' + isSubscribed);
System.debug('Theme: ' + theme);
System.debug('Language: ' + language);
}
}
3. Now, call the parseJsonData method with the JSON data we have:
public class JsonParsingExampleDemo {
public static void main(String[] args) {
String jsonString = '{ "name": "John Doe", "age": 30, "email": "john.doe@example.com", "isSubscribed": true, "preferences": { "theme": "light", "language": "en" } }';
// Call the parseJsonData method to perform deserialization and display the output
JsonParsingExample.parseJsonData(jsonString);
}
}
Output
When you execute the JsonParsingExampleDemo class, the debug logs will show the extracted information from the JSON:
DEBUG|Name: John Doe
DEBUG|Age: 30
DEBUG|Email: john.doe@example.com
DEBUG|Is Subscribed: true
DEBUG|Theme: light
DEBUG|Language: en
Conclusion
In this blog post, we explored the json.deserializeUntyped method in Salesforce, which allows us to parse JSON data into an untyped Object. We walked through a step-by-step example of how to use this method to extract information from a JSON response and display it in the debug logs. This method provides flexibility when working with JSON data in Salesforce, especially when you don't want to create specific Apex classes for deserialization. Remember to cast the untyped object appropriately to access its properties.
I hope you found this blog helpful in understanding the json.deserializeUntyped method in Salesforce. Happy coding!