0

I have a Post method where the other system will be sending me the JSON like this

[{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE(((FirstName = 'Vikram3' AND AssistantName = 'test3' )))",
    "commonIndex": 0,
    "resultSize": 0,
    "result": []
},
{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE (((FirstName = 'Vikram4' AND AssistantName = 'test4' )))",
    "commonIndex": 1,
    "resultSize": 0,
    "result": []
},
{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE (((FirstName = 'Vikram5' AND AssistantName = 'test5')))",
    "commonIndex": 2,
    "resultSize": 0,
    "result": []
    }
 ]

And below is my class which is converted to wrapper obj.

@RestResource(urlMapping='/Portqii/*')
global with sharing class Portqii {
    global class fromJSON {
        public String query;
        public Integer commonIndex; 
        public Integer resultSize;  
        public string result;
    }

  @HttpPost
    global static string  returnRequest(fromJSON jsonBody ) 
    {
        RestRequest     request    = RestContext.request;
        RestResponse    response   = RestContext.response;    
        response.addHeader('Content-Type','applicatin/json');
        fromJSON jsonBody1=jsonBody  ;
        jsonBody1 = (fromJSON) JSON.deserialize(request.requestBody.toString(),fromJSON.class);
        return string.valueof(jsonBody1)  ;
    }
}

And i get this error, while executing it in workbench JSON_PARSER_ERROR message: JSON request body must be an object at [line:1, column:2] errorCode: JSON_PARSER_ERROR

2

So yeah it worked.

you can try this. the issue was your json request is not right.

you were trying to access list of your wrapper class. do this and it will work.

{"jsonBodyList" : your previus json here}

 {"jsonBodyList":
[{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE(((FirstName = 'Vikram3' AND AssistantName = 'test3' )))",
    "commonIndex": 0,
    "resultSize": 0,
    "result": []
},
{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE (((FirstName = 'Vikram4' AND AssistantName = 'test4' )))",
    "commonIndex": 1,
    "resultSize": 0,
    "result": []
},
{
    "query": "SELECT AccountId,Birthdate,FirstName FROM Contact WHERE (((FirstName = 'Vikram5' AND AssistantName = 'test5')))",
    "commonIndex": 2,
    "resultSize": 0,
    "result": []
    }
 ]
}
  • 1
    Dev, why i cannot Pass without "jsonBodyList" directly? give some Gyaan on this. – Arvind Jun 22 '18 at 11:30
  • Even though the JSON you are sending is an array, the object is used by the platform to describe which method parameter the array will be mapped to, in this case the 'jsonBodyList' – sskular May 15 at 14:37
2

In fact the response that you get is a list of fromJSON object.

Below is the correct form (check also the other corrections I've made):

    @RestResource(urlMapping='/Portqii/*')
    global with sharing class Portqii {
      global class fromJSON {
        global String query;
        global Integer commonIndex; 
        global Integer resultSize;  
        global List<string> result;   //it's a list of string actually
    }

  @HttpPost
    global static string  returnRequest(List<fromJSON> jsonBodyList ) 
    {
        // I'm not sure why lines below are included.. seems unnecessary..
        // I've commented out them
        //RestRequest     request    = RestContext.request;
        //RestResponse    response   = RestContext.response;    
        //response.addHeader('Content-Type','applicatin/json');

        // You are parsing it as fromJSON object 
        // and then converting it back to a string on the return statement.. why?!
        //fromJSON jsonBody1 = (fromJSON) JSON.deserialize(request.requestBody.toString(),fromJSON.class);
        //return string.valueof(jsonBody1);

        // instead, you can try to manipulate the jsonBodyList
        for(fromJSON fjs : jsonBodyList){
           ...
           fjs.resultSize = 2; //for e.g.
           ...
        }
        // and then return it in the end
        return JSON.serialize(jsonBodyList);
    }
}

Let me know if it helps

  • I get the same error again.. Yes there were many unnecessary code i forgot to remove it. last 3 lines were important, Updated the question with the latest error, Basically i want to do a qurey which they are sending and append it to result and send it back They will be sending me around 200 JSON(which includes Query also) – Arvind Jun 20 '18 at 10:34
  • @kishore Try the suggested solution from @devendra as: req.setBody('{"jsonBodyList":' + JSON.serialize(jsonBodyList) + '}' and with the above code that I have provided. Let me know if it goes ok. – Enrik Hysko Jun 20 '18 at 14:16
0

Your sample data is an array of object so you should be using List instead of fromJSON while desrializing. Also result variable should be list as well in fromJSON class

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.