[SalesForce] Error : JSON request body must be an object

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

Best Answer

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": []
    }
 ]
}
Related Topic