1

I develop an Apex REST web service in Salesforce and I would like to test it with Postman.

Unfortunately, I get an error message than I don't understand:

Expected JSON object to deserialize apex parameter from at [line:3, column:14]

This is the JSON I tr to send with postman:

    {
      "data":  [
            {
                  "attendantYear":"2014;2015;2016;2018;2019",
                  "birthDate":"1981-12-11",
                  "city":"New-York",
                  "country":"USA",
                  "email":"[email protected]",
                  "firstname":"Jean-Paul",
                  "itra":450,
                  "lastname":"Gus",
                  "salutation":"Mr."
            }
      ]
}

And this is how looks like my apex web service:

@RestResource(urlMapping='/accounts/*')
global with sharing class ABT_WSAccounts{

    @HttpPost
    global static String upsertAccounts(String data){
        String result;

        System.debug('--- data : ' + data);

        if(!String.isBlank(data)){
            List<Account> accountsToUpsert = new List<Account>();

            List<ABT_AccountInnerClass> sobjects = (List<ABT_AccountInnerClass>) JSON.deserialize(data, List<ABT_AccountInnerClass>.class);
            System.debug('--- sobjects : ' + sobjects);

            .....
        }

        return result;
    }
}

And my inner class:

global class ABT_AccountInnerClass{

    global String firstname;
    global String lastname;
    global String salutation;
    global Date birthDate;
    global String email;
    global String street;
    global String country;
    global String attendantYear;
    global Decimal itra;

}

Please, can you explain me the error?

Thanks

1
  • 1
    You're downcasting it to List of SObject. So it expects a list of objects in the JSON but it's not. Either append '[' & ']' at the beginning & end of the JSON or cast it to an SObject instance instead of a List. ABT_AccountInnerClass sobjects = (ABT_AccountInnerClass)JSON.deserialize(jsonString, ABT_AccountInnerClass.class); Commented Oct 27, 2020 at 5:14

1 Answer 1

1

Since you typed the parameter as a string, you have to encode the payload as JSON:

{
 "data": "[ { \"attendantYear\":\"2014;2015;2016;2018;2019\",\"birthDate\":\"1981-12-11\",\"city\":\"New-York\",\"country\":\"USA\",\"email\":\"[email protected]\",\"firstname\":\"Jean-Paul\",\"itra\":450,\"lastname\":\"Gus\",\"salutation\":\"Mr.\"}]"
}

It would be much easier to instead just pass in the parameter directly:

global static String upsertAccounts(List<ABT_AccountInnerClass> data){

This would let you use your JSON as written.

1
  • Why I didn't thought about it. It works, thanks.
    – Oupat
    Commented Oct 27, 2020 at 9:52

Not the answer you're looking for? Browse other questions tagged .