Deserialize JSON with Reserved Keywords in Apex?

Deserialize JSON with Reserved Keywords in Apex?

On July 12, 2025, Posted by , In Apex,Salesforce, With Comments Off on Deserialize JSON with Reserved Keywords in Apex?

Question

I need to deserialize a JSON string into an Apex object, but some of the property names in the JSON are reserved words in Apex. For example:

String jsonString = '{"currency" : "ABC"}';

public class JSONResult {
    public String currency;
}

JSONResult res = (JSONResult) JSON.deserialize(jsonString, JSONResult.class);
System.debug(res);

However, this results in a compilation error because “currency” is a reserved keyword in Apex. How can I handle this issue while still deserializing the JSON properly?

Answer

There is no direct way to map reserved keywords to Apex class properties during deserialization, but here are two possible workarounds:

1. Modify the JSON String Before Deserialization

One approach is to replace the reserved property name in the JSON string before deserializing it. This method works if the structure of the JSON is known and consistent.

String jsonString = '{"currency" : "ABC"}';
jsonString = jsonString.replace('"currency":', '"currency_x":');

public class JSONResult {
    public String currency_x;
}

JSONResult res = (JSONResult) JSON.deserialize(jsonString, JSONResult.class);
System.debug(res.currency_x); // Outputs: ABC

Explanation:

The original JSON contains the key "currency", which is a reserved word in Apex.
We use jsonString.replace('"currency":', '"currency_x":') to rename "currency" to "currency_x".
The Apex class JSONResult has a field named currency_x to match the modified JSON key.
After deserialization, we can access res.currency_x, which holds the value "ABC".

Pros:

Simple and quick solution.

Cons:

If "currency" appears in a string inside the JSON, it may be unintentionally replaced.

2. Use the JSON Parser Methods Manually

A more precise but complex approach is to manually parse the JSON using JSONParser. This avoids unintended replacements but requires more effort.

String jsonString = '{"currency" : "ABC"}';
JSONParser parser = JSON.createParser(jsonString);

String currencyValue;
while (parser.nextToken() != null) {
    if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'currency') {
        parser.nextToken(); // Move to the value
        currencyValue = parser.getText();
    }
}

System.debug(currencyValue); // Outputs: ABC

Explanation:

We create a JSONParser to manually parse the JSON string.
The loop iterates through the JSON tokens.
When it finds the "currency" key, it moves to the next token (the value) and stores it in currencyValue.
Finally, System.debug(currencyValue); prints "ABC".

Pros:

Precise, with no risk of unintended replacements.

Cons:

More complex and requires manual parsing logic.

If the JSON structure is simple and controlled, modifying the JSON string before deserialization is a quick fix. However, for more complex cases, manually parsing the JSON ensures accuracy and prevents unintended replacements.

Enroll for Career-Building Salesforce Training with Real-Time Projects

Our Salesforce course is designed to give you a deep understanding of the Salesforce platform, equipping you with the essential skills needed to excel in the CRM industry. The program covers key modules like Salesforce Admin, Developer, and AI, combining theoretical knowledge with hands-on experience. Through real-world projects and interactive exercises, you’ll gain the expertise to tackle complex business challenges using Salesforce solutions. Our expert instructors ensure you acquire both technical proficiency and industry insights to thrive in the Salesforce ecosystem.

In addition to technical training, our Salesforce Training in Chicago provides personalized mentorship, certification guidance, and interview preparation to enhance your career prospects. You’ll have access to extensive study materials, hands-on project work, and dedicated support throughout your learning journey. By the end of the course, you’ll be fully prepared for certification exams and equipped with real-world problem-solving skills that employers value. Take the next step in your Salesforce career—sign up for a Free Demo today!

Comments are closed.