5

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 ?

@RestResource(urlMapping='/kh/*')
global with sharing class RESTKhController {
    @HttpGet
    global static ResponseWrapperClass getList() {
        return new ResponseWrapperClass();
    }

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

    global class DataWrapperClass {
        global String id;
    }
}
2

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

  • That a good idea. Seem this is the only way ? – liratanak Sep 18 '15 at 4:31
  • Yes, this is the one way I can think of. The other way is not to use reserved words. :) – manjit5190 Sep 19 '15 at 18:26
3

Use JSON2Apex with the 'Create explicit parse code' option enabled. This will produce code like so:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class MyList {
    // ...boilerplate...

    public class List_Z {
        public String id {get;set;}

        public List_Z(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'id') {
                            id = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'List_Z consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public List<List_Z> list {get;set;}

    public MyList(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'list') {
                        list = new List<List_Z>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            list.add(new List_Z(parser));
                        }
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }

    //...etc...

Just replace all occurrences of the list member variable with list_z (or whatever you like), and you're off to the races:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
// Hand-modified by @metadaddy :-)
//

public class MyList {
    // ...boilerplate...

    public class List_Z {
        public String id {get;set;}

        public List_Z(JSONParser parser) {
            while (parser.nextToken() != JSONToken.END_OBJECT) {
                if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                    String text = parser.getText();
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'id') {
                            id = parser.getText();
                        } else {
                            System.debug(LoggingLevel.WARN, 'List_Z consuming unrecognized property: '+text);
                            consumeObject(parser);
                        }
                    }
                }
            }
        }
    }

    public List<List_Z> list_z {get;set;}

    public MyList(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'list') {
                        list_z = new List<List_Z>();
                        while (parser.nextToken() != JSONToken.END_ARRAY) {
                            list_z.add(new List_Z(parser));
                        }
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }

    //...etc...

Note - you'll also need to move the test code into its own class. Superfell or I need to go back and fix that. We should also rename the member variable when it clashes with a reserved word.

Anyway - now you can create a MyList from JSON like so:

MyList l = MyList.parse(json);
-1

This is 3 years late, but if anyone ever sees this, you can also use responseString = response.getBody() and make a string of the response, then use responseString.Replace('"reservedWord":', '"yourNewWord":') to map response fields to a wrapper class.

  • Hi, just curious I see this post was downvoted. Is replacing string in response bad practice? – helpifell Sep 4 '19 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.