When working with APIs and external web services in Salesforce Apex, parsing JSON responses is a daily task. Apex provides two distinct ways to deserialize JSON data into accessible properties: Typed Deserialization using a Wrapper Class and Untyped Deserialization using Maps.
.get('key') when the JSON structure is dynamic or unpredictable.
Approach 1: Strongly-Typed Deserialization (Dot Notation)
If you define a custom Apex wrapper class, you can deserialize JSON directly into strongly-typed object instances. Members are then accessed exclusively using dot notation.
Given the following JSON string:
String jsonData = '{"name": "John", "age": 30}';
Define a wrapper class matching the JSON structure and deserialize the string:
// 1. Define the wrapper class
public class Person {
public String name;
public Integer age;
}
// 2. Deserialize into a strongly-typed object
Person personObject = (Person) JSON.deserialize(jsonData, Person.class);
// 3. Access values using dot notation
String personName = personObject.name; // "John"
Integer personAge = personObject.age; // 30
personObject.get('name') on a custom Apex class instance will cause a compile-time error (Method does not exist or incorrect signature). Custom Apex classes do not implement a get() method or map indexers.
Approach 2: Untyped Deserialization (Key-Based Lookup)
If you don't want to create a dedicated wrapper class or if the incoming JSON structure varies dynamically, use JSON.deserializeUntyped() to parse the payload into a Map<String, Object>. Keys are accessed using the map's .get() method with explicit type casting.
// 1. Deserialize into an untyped map
Map<String, Object> personMap = (Map<String, Object>) JSON.deserializeUntyped(jsonData);
// 2. Access members using .get('key') with type casting
String personName = (String) personMap.get('name'); // "John"
Integer personAge = (Integer) personMap.get('age'); // 30
(String) personMap.get('name')), as untyped values return as generic Object types.
Which Approach Should You Use?
- Use Wrapper Classes (Dot Notation) when: You know the JSON payload structure in advance, need strict compile-time type checking, and want cleaner, readable code without manual type casting.
- Use Untyped Maps (
.get()) when: You are processing dynamic keys, extracting only a few specific attributes from massive payloads, or building generic integration utilities.
Conclusion
Understanding the boundary between typed objects and untyped maps ensures clean, error-free JSON handling in Apex. Choose wrapper classes for structure and type safety, or untyped maps for flexible parsing.