In our Salesforce org, we were working on integrating our Salesforce instance with a third-party ticketing system for managing customer support cases. The goal was to sync the status of support cases between Salesforce and this external system using an API.
We need to send the current case status to the third-party system via API. Since enums are more efficient for managing statuses, we decided to serialize the enum into a JSON format.
In this Salesforce tutorial, I will explain how to serialize and deserialize Enums in JSON Apex.
Serialize and Deserialize Enums in JSON in Salesforce Apex
To serialize and deserialize enums in JSON in Salesforce Apex, we will use the JSON.serialize() nad JSON.deserialize() methods.
First, we will create an apex class with enum values that we will serialize and deserialize in the further steps.
Create a new Apex class in the Salesforce developer console by selecting File > New > Apex Class. After this, enter the code in the new Apex class.
public class Enumserialization {
public enum Status {
Active,
Inactive,
Pending
}
}Now, we will serialize the ENUM by converting it into a JSON string. To do that, enter the code below in the apex class.
public class EnumSerialization {
public enum Status {
Active,
Inactive,
Pending
}
public static void serializeEnum() {
Status currentStatus = Status.Active;
String jsonString = JSON.serialize(currentStatus);
System.debug('Serialized Enum: ' + jsonString);
}
}
To deserialize the ENUM, we will use JSON.deserialize() method, enter the code below the existing serialization method.
public static void deserializeEnum(String jsonString) {
Status status = Status.valueOf(jsonString.replace('"', ''));
System.debug('Deserialized Enum: ' + status);
}Now, save the apex class and launch the anonymous window. Then, enter the code below in the anonymous window.
EnumSerialization.serializeEnum();
EnumSerialization.deserializeEnum('"Active"');In the execution log window, we can see the output for the serialized and deserialized values in the debug.

This way, we can serialize and deserialize the Enums in JSON in Salesforce Apex using the JSON.serialize and JSON.deserialize methods.
Using Enum.valueOf for Deserialization of Enum in Salesforce Apex
In Salesforce Apex, we can use the Enum.valueOf method to directly convert a string into the corresponding enum value. This approach is useful when you want to avoid JSON.deserialize method.
To deserialize an Enum using the Enum.Use the valueOf method, open the Salesforce developer console, and follow the steps below.
In the Salesforce developer console, create a new Apex class and enter the following.
public class DeserializeEnum {
public enum Status {
Active,
Inactive,
Pending
}
public static void deserializeWithValueOf(String statusString) {
Status status = Status.valueOf(statusString);
System.debug('Deserialized Enum using valueOf: ' + status);
}
}In the above code, deserializeWithValueOf takes a single parameter, statusString, that will be a string representation of one of the enum values.
After this, statusString is deserialized into the corresponding Status enum value using the valueOf method.
The valueOf method checks if the string matches one of the enum constants. If it does, it returns the matching enum value. If it doesn’t match, it will throw a System.TypeException.
Launch the Apex anonymous window and call the Apex class using the code below to get the output.
- After entering the anonymous code, execute it.
- In the execution log window, we can see the output of the JSON string as Active, which is a deserialized ENUM value.

This way, we can deserialize ENUMS in Salesforce Apex using the Enum.valueOf method.
Serialize and Deserialize ENUM using Custom Serialization in Salesforce Apex
In Salesforce Apex, we can also use custom serialization logic when we need to include extra information, such as numeric codes, along with the enum value in the JSON.
In the customized serialization, we can create a custom format, such as serializing the enum value to a numeric representation or adding additional context.
To serialize ENUM using custom serialization, open the Salesforce developer console, then create a new Apex class. After that, enter the code below and save it.
public class EnumSerialization{
public enum Status {
Active(1),
Inactive(2),
Pending(3);
public Integer code;
Status(Integer code) {
this.code = code;
}
}
public static String customSerialize(Status status) {
return '{"status":"' + status + '", "code":' + status.code + '}';
}
public static Status customDeserialize(String jsonString) {
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
String statusString = (String) jsonMap.get('status');
return Status.valueOf(statusString);
}
}In the above code, the ENUM “status” has values ‘active,’ ‘inactive,’ and ‘pending.’ along with an integer value associated with them like ‘active(1)’.
The method “customSerialize” serializes the Status enum to a JSON string. It manually constructs a JSON representation by combining the enum’s name, “Active“, and its associated code ‘1’ into a string. Then it will return a JSON string like {“status”: “Active”, “code”:1}.
The deserialize method JSON.deserializeUntyped deserializes the JSON string into a Map collection, allowing access to the values by key.
Now, open the anonymous apex window and execute the code below to call the serialize and deserialize enum methods.
String jsonString = CustomEnumSerialization.customSerialize(CustomEnumSerialization.Status.Active);
System.debug('Custom Serialized JSON: ' + jsonString);
CustomEnumSerialization.Status status = CustomEnumSerialization.customDeserialize('{"status":"Inactive", "code":2}');
System.debug('Custom Deserialized Enum: ' + status); Now, in the execution log window, we can see the serialized and deserialized enum values.

This way, we can serialize and deserialize the Enums in JSOn using the above-customized method in Salesforce Apex.
Conclusion
In this Salesforce tutorial, we have learned about the various methods to serialize and deserialize the ENUM values with JSON. We used general methods like JSON.serialize() and JSON.deserialize(), and after this, we discussed other methods, such as Enum.valueOf and JSON.deserializeUntyped to serialize and deserialize the Enums in Salesforce Apex.
You may also like to read.
- Convert JSON to String in Salesforce Apex
- Convert ID to String in Salesforce Apex
- Create Set Collection in Salesforce Apex
- Convert a JSON String to a Map in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.