[SalesForce] Apex REST response with reserved words

I follow Creating REST APIs

And I would like to response data as:

    "list": [
            "id": "KH001"

My Class look like this
( everything just work as normal if I used items instead of list as variable name )
I am wondering would be possible if the response above is really required ?

global with sharing class RESTKhController {
    global static ResponseWrapperClass getList() {
        return new ResponseWrapperClass();

    global class ResponseWrapperClass {
        global List<DataWrapperClass> list; // ERROR unexpected token: 'list'

    global class DataWrapperClass {
        global String id;

Best Answer

Salesforce Apex and other languages like Java, C etc will give you errors when you try to use reserved words.

However as you said that this response is really required so here is what I suggest.

  1. Rename the list to something unique example: list_replace_me

    global List<DataWrapperClass> list_replace_me;
  2. Instead of returning the wrapper from your GET method, return a string.

    global static string getList() { 
    //Manually serialize this object
    new ResponseWrapperClass(); } 
  3. Manually serialize the object by JsonGenerator class to get a string, or use JSON.serialize(inputobject) which return a string.

  4. Replace "list_replace_me" with "list" using String.replace function.

  5. Return the new String after replacing.

Json Generator

Related Topic