function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris Baker 9Chris Baker 9 

Unexpected parameter encountered during deserialization

    

I am trying to write a REST service which will send SF objects between Orgs. Right now, as a test, I am sending data to the same Org that it is coming from. I wrote a GET request which worked so I know the communcation part is working. But I am now trying to send an Account using POST. My JSON looks like this

    
{
    "acct": {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v34.0/sobjects/Account/0018A000002ryhSQAQ"
        },
        "Id": "0018A000002ryhSQAQ",
        "IsDeleted": false,
        "Name": "Test",
        "RecordTypeId": "012C0000000QakwIAC",
        "BillingAddress": null,
        "ShippingAddress": null,
        "OwnerId": "0058A000000I5WbQAK",
        "CreatedDate": "2015-08-25T16:07:17.000+0000",
        "CreatedById": "0058A000000I5WbQAK",
        "LastModifiedDate": "2015-08-25T16:07:17.000+0000",
        "LastModifiedById": "0058A000000I5WbQAK",
        "SystemModstamp": "2015-08-25T16:07:17.000+0000",
        "LastViewedDate": "2015-08-28T13:54:38.000+0000",
        "LastReferencedDate": "2015-08-28T21:54:40.000+0000",
        "IsPartner": false,
        "IsCustomerPortal": false,
        "Advocacy__c": false,
        "Background_check_required__c": "Criminal",
        "External_Content_Approval_Required__c": false,
        "Indicative_Data_Options__c": "Name",
        "Internal_Content_Approval_Required__c": false
    }
}






**This is the service**
@RestResource(urlMapping='/ClientMigration')
global with sharing class ClientMigrationService {
    static Account clientMigrated;
    @HttpPost
    global static string Migrate(Account Client) {
        //Account a = JSON.deserialize(client);
        /*Map<String, SObject> mapJsonObjects = (Map<String, SObject>)
            JSON.deserialize(Client, Map<String, SObject>.class);
        Account a= (Account) mapJsonObjects.get('Account');   
        
        insert a;
        clientMigrated = a;
        */
        system.debug (client);
        return 'Success';
    }
    
     public class RequestWrapper{
        Account acct;
        public RequestWrapper(){}
    }        
        
 }


    

This is the code calling the service


    
public PageReference Migrate() {

        Account myAct = accountInfo ;

        system.debug('myAct = ' + myAct);

        RequestWrapper rw = new RequestWrapper();

        rw.acct = myAct;

        string myActJSON = JSON.serialize(rw);
        system.debug('myActJSON = ' + myActJSON );
        
        sendAccountDetails(myActJSON );
        return null;
    }
    
    private string sendAccountDetails(string actDetails) {
        HttpRequest req = new HttpRequest();

        req.setMethod('POST');
       
        string endpoint = oauth.instance_url +'/services/apexrest/ClientMigration';
        system.debug('endpoint = ' + endpoint);
        req.setEndpoint(endpoint);
       

        req.setBody(actDetails);
        req.setCompressed(true); 
        req.setHeader('Authorization', 'OAuth '+ oauth.access_token);
        req.setHeader('content-type', 'application/json');

        Http http = new Http();

        HTTPResponse res = http.send(req);

        System.debug('BODY: '+res.getBody());

        System.debug('STATUS:'+res.getStatus());

        System.debug('STATUS_CODE:'+res.getStatusCode());

        return res.getBody();

    }

 public class RequestWrapper{
        Account acct;
        public RequestWrapper(){}
    }





This is the error I keep seeing
Unexpected parameter encountered during deserialization: acct at [line:1, column:10]","errorCode":"JSON_PARSER_ERROR"}]

It seems that no matter what I do it complains at the location in the JSON where it get just past the very first object and gets into the inner objects. I tried doing this without the RequestWrapper and just serializing the account directly but had the same problem. I think I must be doing something wrong with the serialization process.

Does anyone have some suggestions?
Thanks,
Chris
Anirudh SinghAnirudh Singh
Hi Chris,

In the POST Method,

try this:

@HttpPost
global static string Migrate(RequestWrapper Client)
{
...processing code...
}

Thanks and Regards,
Anirudh Singh
ApuroopApuroop
I know this is too late but maybe it helps someone..

In the JSON's key value, you have to have the exact string as you specify in your HttpPost method, like below:
{
    "Client": {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v34.0/sobjects/Account/0018A000002ryhSQAQ"
        },
        "Id": "0018A000002ryhSQAQ",
        "IsDeleted": false,
        "Name": "Test",
        "RecordTypeId": "012C0000000QakwIAC",
        "BillingAddress": null,
        "ShippingAddress": null,
        "OwnerId": "0058A000000I5WbQAK",
        "CreatedDate": "2015-08-25T16:07:17.000+0000",
        "CreatedById": "0058A000000I5WbQAK",
        "LastModifiedDate": "2015-08-25T16:07:17.000+0000",
        "LastModifiedById": "0058A000000I5WbQAK",
        "SystemModstamp": "2015-08-25T16:07:17.000+0000",
        "LastViewedDate": "2015-08-28T13:54:38.000+0000",
        "LastReferencedDate": "2015-08-28T21:54:40.000+0000",
        "IsPartner": false,
        "IsCustomerPortal": false,
        "Advocacy__c": false,
        "Background_check_required__c": "Criminal",
        "External_Content_Approval_Required__c": false,
        "Indicative_Data_Options__c": "Name",
        "Internal_Content_Approval_Required__c": false
    }
}
I just changed acct to Client. JS is case sensitive and we don't want to take any chances. Anyway this was my error.