3

I searched all over but not found the right one.

My Body string below,

[{"revokePermSetNames":["RemovPerm1","RemovPerm2"],
  "EmployeeNumber":"8507",
  "assignPermSetNames":["AddPerm1","AddPerm2"]},
 {"revokePermSetNames":["RemovPerm3","RemovPerm4"],
  "EmployeeNumber":"8508",
  "assignPermSetNames":["AddPerm3","AddPerm4"]}]

My Class here

@HttpPost  
global static void processPermSetDelegation() {  
    System.debug('****'+RestContext.request);
    System.debug('^^^^'+RestContext.request.requestBody.toString());
    System.debug(String.valueOf(RestContext.request.params));

    List<permissionDataWrapper>  delegationMap = (List<permissionDataWrapper>)JSON.deserialize(RestContext.request.requestBody.toString(),permissionDataWrapper.class); 
    for(permissionDataWrapper cp : delegationMap) {
        System.debug('------'+cp.EmployeeNumber);
        for(string s: cp.assignPermSetNames )
            System.debug('Assign :------'+s);
        for(string a: cp.revokePermSetNames)
            System.debug('Revoke :------'+a);

        System.debug('------------------------------------------');
    }    
}

global class permissionDataWrapper {
    public String EmployeeNumber {get;set;}
    public Set<String> assignPermSetNames {get;set;}
    public Set<String> revokePermSetNames {get;set;}
    permissionDataWrapper () {
        assignPermSetNames = new Set<string>();
        revokePermSetNames = new Set<string>();
    }
}   

Error :

Malformed JSON: Expected '{' at the beginning of object

  • 1
    This line is causing the issue: {"revokePermSetNames":"RemovPerm3","RemovPerm4"], – itzmukeshy7 Jun 14 '17 at 11:25
  • In this line, you forgot to add the starting square bracket [ to start array. – itzmukeshy7 Jun 14 '17 at 11:26
  • Yeah that's the only error I encountered as well, though with a different message. – Adrian Larson Jun 14 '17 at 11:27
  • How are you passing the body? – itzmukeshy7 Jun 14 '17 at 11:27
  • I have pasted wrong body. I will edit now. – bharath Jun 14 '17 at 11:28
9

Try this ;)

List<permissionDataWrapper>  delegationMap = (List<permissionDataWrapper>)JSON.deserialize(RestContext.request.requestBody.toString(), List<permissionDataWrapper>.class);

You need to pass the List<permissionDataWrapper>.class not permissionDataWrapper.class for the 2nd parameter(JSON.deserialize(1st, 2nd)) because you are processing a list not a single item.

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.