why below REST web service does not properly deserialize the input argument?Please help me out?

        global class HH_SurveyController {

      public static JSON2Apex parse(String json) 
            return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);

        global static List<Test_Survey__c> getSurvey() {
            RestRequest req=RestContext.request;
            RestResponse res=RestContext.response;
             res.addHeader('Content-Type', 'application/json'); 
             String surveyid =req.requestURI.substring(req.requestURI.lastIndexof('/')+1);
            List<Test_Survey__c> Survey= [SELECT Id,Test_Hamlet_Segment_Camp__c,Test_Field_Investigator_Name__c,Test_Supervisor_Name__c,Test_Respondent_Name__c,Test_Consent_Status__c from Test_Survey__c];
            return Survey;

        global static String createNewSurvey(String Hamlet_Segment_Camp,String Field_Investigator_Name,String Supervisor_Name,String Respondent_Name,String Consent_Status) 
            Test_Survey__c t = new Test_Survey__c();

            t.Test_Hamlet_Segment_Camp__c = Hamlet_Segment_Camp;
            t.Test_Field_Investigator_Name__c = Field_Investigator_Name;
            t.Test_Supervisor_Name__c= Supervisor_Name;
            t.Test_Respondent_Name__c= Respondent_Name;
            t.Test_Consent_Status__c= Consent_Status;

           // w.Test_Date_of_the_survey__c= Date_of_the_survey;

            insert t;

            return t.id;

test class

    public class JSON2Apex_Test {

      public static testMethod void testParse() {
            String json = '{ '+
            '  \"Test_Hamlet_Segment_Camp\" : \"Hamlet\",'+
            '  \"Test_Field_Investigator_Name\" : \"Investigator \",'+
            '  \"Test_Supervisor_Name\" : \"Supervisor\",'+
            '  \"Test_Respondent_Name\" : \"Respondent \",'+
            '  \"Test_Consent_Status\" : 1'+
            '       }'+


            JSON2Apex obj = JSON2Apex.parse(json);
            System.assert(obj != null);

  • Can you remove all backslash \ from json and try.
    – Reshma
    Jan 18, 2019 at 10:23

1 Answer 1


Ensure that the JSON names match the class property names.

So instead of e.g.:

        '  \"Test_Hamlet_Segment_Camp__c\" : \"Hamlet\",'+

it should be:

        '  \"Test_Hamlet_Segment_Camp\" : \"Hamlet\",'+

to match the property name:

    public String Test_Hamlet_Segment_Camp;


Looked at your code again. Using the @HttpPost annotation signals that the platform should automatically deserialize JSON input into the parameters of the methods - you do not need to write code for that so the JSON2Apex class is redundant.

In unit tests, you just directly call the createNewSurvey method passing the individual values. In normal use, the JSON you would need to send is:

    "Test_Hamlet_Segment_Camp" : "Hamlet",

i.e. the args level is not needed.

  • i have corrected my test class but still facing this "Unexpected parameter encountered during deserialization" Error
    – Sonal
    Jan 18, 2019 at 10:11
  • i have removed the JSON2APEX class & args level but still facing same issue
    – Sonal
    Jan 18, 2019 at 10:40
  • P.S You don't escape " in Apex, since it is not a reserved character.
    – sfdcfox
    Jan 18, 2019 at 11:08

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.