0

Please see the below code I have done some modification,

@RestResource(urlMapping='/instructionsRestCall/*') global with sharing class InstructionsRestService { @HttpGet global static List getInstructions(){

    RestRequest request = RestContext.request;
    RestResponse response = RestContext.response;

    String instId = request.requestURI.subString(request.requestURI.lastIndexOf('/'));
    //Send list of Instruction details as per the provided JSON format for Instruction records where Category Type Code (catTypeCd__c) is not null
    List<Instructions__c> instList = [select Country__c,Category__c,SubCategory__c,Requirements__c,catTypeCd__c from Instructions__c];
    system.debug('Instructions List->>>>'+instList);      

    List<requestTypes> catWrapList = new List<requestTypes>();

    List<countryWrapper> counWrapList = new List<countryWrapper>();
    for(Instructions__c ins : instList){
        if(ins.catTypeCd__c != null){

            countryWrapper countWrap = new countryWrapper();
            requestTypes catWrap = new requestTypes();
            caseTypes innWrap = new caseTypes();

            countWrap.corpCode = ins.Country__c;
            catWrap.name = ins.Category__c;
            innWrap.name = ins.SubCategory__c;
            innWrap.value = ins.Requirements__c;
            innWrap.categoryTypeCode = ins.catTypeCd__c;
            countWrap.requestTypes = catWrap;
           // countWrap.innerWrapper = innWrap;
            countWrap.requestTypes.caseTypes = innWrap;
            counWrapList.add(countWrap);
        }    
    }
    system.debug('Result from WrapperList->>>>'+counWrapList);
    return counWrapList;
    //return JSON.serialize(counWrapList);
}        

global class countryWrapper{
    public String corpCode{get;set;}
    public requestTypes requestTypes{get;set;}
    //public innerWrapper innerWrapper{get;set;}
}
global class requestTypes{
    public String name{get;set;}
    public caseTypes caseTypes{get;set;}
}
global class caseTypes{
    public String name{get;set;}
    public String value{get;set;}
    public Double categoryTypeCode{get;set;}
}

}

I want this this corpCode will be in first position and after that requestType. What is wrong with my wrapper. Thanks in advance,

enter image description here

1

You cannot generate the format specified because it is not valid JSON.

A map always requires a key for each element, and you are missing that structure here:

        {
            "something",
             [
                {
                    Subcategory__c: "something",
                    Requirements__c: "something",
                    catTypeCd__c: "1234",
                }
            ]
        }

More generally, if you want to generate a JSON map, you need to be serializing an Apex object (where each field becomes a key, and its value the corresponding value), or an actual Apex Map instance. As you can see in your generated JSON, this structure:

global class categoryWrapper{
    public String category{get;set;}
    public innerWrapper innerWrapper{get;set;}
}

does not produce a JSON map where the key category maps to the value of innerWrapper. To get a result like that, you'd need something along the lines of

global class categoryWrapper{
    public innerWrapper category {get;set;}
}
| improve this answer | |
  • What are you asking? – David Reed Mar 31 at 14:58
  • Hi David, thanks for your reply. I have now getting the following response- [ { "requestTypes" : { "name" : "On Road Programs - Taxable Benefit-FA", "caseTypes" : { "value" : null, "name" : "Question", "categoryTypeCode" : 401.0 } }, "corpCode" : "USA" } ] All I need to serialize the json structure like first "corpCode":"USA" after that rest of things. – Rahul Mar 31 at 14:59
  • Okay, did you see the details in my answer above about how JSON serialization works? – David Reed Mar 31 at 15:00
  • so my final json structure - [ { "corpCode" : "USA", "requestTypes" : { "name" : "On Road Programs - Taxable Benefit-FA", "caseTypes" : { "value" : null, "name" : "Question", "categoryTypeCode" : 401.0 } } } ] – Rahul Mar 31 at 15:00
  • Here is my wrapper- global class countryWrapper{ public String corpCode{get;set;} public requestTypes requestTypes{get;set;} //public innerWrapper innerWrapper{get;set;} } global class requestTypes{ public String name{get;set;} public caseTypes caseTypes{get;set;} } global class caseTypes{ public String name{get;set;} public String value{get;set;} public Double categoryTypeCode{get;set;} } – Rahul Mar 31 at 15:02

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.