Recently, in our Salesforce Org, we were developing an application that integrates with an external API to fetch customer information. The API returns customer details in JSON format, so we have to parse this JSON string to store it in a Map for access within the Apex code.
In this Salesforce tutorial, I will explain various methods to convert a JSON string to a Map in Salesforce Apex.
JSON Handling in Salesforce
In Salesforce, handling JSON effectively is required to integrate the Salesforce org with external APIs, configure dynamic data exchanges, and manage complex metadata structures.
Parsing JSON into Maps or custom objects allows us to build efficient and maintainable Apex code while ensuring connections between the systems.
Convert JSON String to Map Using JSON.deserializeUntyped() in Salesforce Apex
To convert a JSON string to a map using the JSON.deserializeUntyped() method, navigate to the Salesforce developer console, and follow the steps below.
In the developer console, create a new Apex class, enter the code below, and save it.
public class JSONstringConversion {
public static void convertJsonToMap() {
String jsonString = '{"name": "Eva James", "age": 30, "isActive": true}';
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
String name = (String) resultMap.get('name');
Integer age = (Integer) resultMap.get('age');
Boolean isActive = (Boolean) resultMap.get('isActive');
System.debug('Name: ' + name);
System.debug('Age: ' + age);
System.debug('Is Active: ' + isActive);
}
}In the above code, we first defined a JSON string and then deserialized the JSON to a Map using the JSON.deserializeUntyped() method.
After this, the resultMap.get() method will assess the JSON string values and then display them in a Map in the debug.
Now launch the Apex anonymous window and execute the code below to call the class method.
JSONstringConversion.convertJsonToMap();Now, in the execution log window, we can see the Map values converted from a JSON string.

This way, convert a JSON String to a Map using the JSON.deserializeUntyped() method in Salesforce Apex.
Convert JSON String to Map Using Wrapper Class in Salesforce Apex
To convert a JSON string to a Map using a wrapper class in Salesforce Apex, navigate to the developer console and follow the steps below.
In the developer console, create a new Apex class, enter the code below, and save it.
public class ConvertList {
public TitleWrapper titleIns { get; set; }
public ProductWrapper productIns { get; set; }
public ConvertList() {
titleIns = new TitleWrapper();
productIns = new ProductWrapper();
}
public class TitleWrapper {
public String Title { get; set; }
}
public class ProductWrapper {
public String Product_Type { get; set; }
}
public static void deserializeJSON() {
String fields = '[{"titleIns":{"Title":"Name"}, "productIns":{"Product_Type":"IsActive"}}]';
List<ConvertList> convertListObj = (List<ConvertList>)System.JSON.deserialize(fields, List<ConvertList>.class);
System.debug(convertListObj);
}
}Now, call the above class method using the code below in the Apex anonymous window.
ConvertList.deserializeJSON();Output:
As we call the class through the anonymous code, we will see the output in the execution log window. The debugging will convert the JSON string values to the map value.

This way, we can convert the JSON string to Map values in Salesforce Apex using the wrapper class method.
Convert JSON String to Map using JSON.deserialize() method in Salesforce Apex
In Salesforce, JSON.deserialize() does not directly support deserializing in a map. Instead, we need to define a custom Apex class representing the JSON structure, or we can use JSON.deserializeUntyped, as we discussed in the above example.
The alternate way is to create an Apex class that represents the JSON structure, as shown in the example below.
Like if the JSON string is{“name”: “John”, “age”: 30, “city”: “New York”}, we will create the class in the following way.
Anonymous Apex code:
public class Person {
public String name;
public Integer age;
public String city;
}Deserialize using the JSON.deserialize() method
String jsonString = '{"name":"Steve", "age":30, "city":"New York"}';
Person person = (Person) JSON.deserialize(jsonString, Person.class);
System.debug('Name: ' + person.name);
System.debug('Age: ' + person.age);
System.debug('City: ' + person.city);Output:

This way, we can convert a JSON string to a map using the deserialize method, where we need to define an Apex class that matches the JSON structure.
Convert Nested JSON Strings to Map in Salesforce Apex
In Salesforce Apex, parsing complex or nested JSON structures often requires combining multiple approaches.
For example, we have a nested JSON string with the following structure.
{
"user": {
"name": "Alice",
"details": {
"age": 29,
"isActive": true
}
},
"roles": ["Admin", "Editor"]
}We can execute the code below to convert this nested JSON string to a map.
String jsonString = '{"user":{"name":"Alice","details":{"age":29,"isActive":true}},"roles":["Admin","Editor"]}';
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
Map<String, Object> user = (Map<String, Object>) resultMap.get('user');
String name = (String) user.get('name');
Map<String, Object> details = (Map<String, Object>) user.get('details');
Integer age = (Integer) details.get('age');
Boolean isActive = (Boolean) details.get('isActive');
List<Object> roles = (List<Object>) resultMap.get('roles');
System.debug('Name: ' + name);
System.debug('Age: ' + age);
System.debug('Is Active: ' + isActive);
System.debug('Roles: ' + roles);In the above anonymous code, we have deserialized a JSON string into an untyped map using JSON.deserializeUntyped(), then retrieved and converted values from the map to specific types (String, Integer, Boolean).
Output:

This way, we can convert a nested or complex JSON string to a Map using the above method.
Conclusion
In this Salesforce tutorial, we have learned how to convert a JSON to a Map in Apex using methods such as JSON.deserializeUntyped(), wrapper classes, and JSON.deserialize(), which can efficiently parse JSON strings. The JSON.deserializeUntyped() method quickly deserializes JSON into an untyped map, while wrapper classes help parse complex JSON data.
You may also like to read:
- Convert JSON to String in Salesforce Apex
- Serialize and Deserialize Enums in JSON in Salesforce Apex
- Convert Date to String in Salesforce Apex
- SOQL Query in Apex Programming

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.