1

I am trying to read the JSON request in my REST service in Apex class. I am getting the JSON_PARSER_ERROR as below.

"message": "Unexpected parameter encountered during deserialization: accid at [line:3, column:14]"

I believe I am doing something wrong (obviously :) ) while reading the JSON string in the class.

Can anyone guide me where I'm going wrong. Checked the herokuapp. Its parsing the string using JSON parser and tokens. http://json2apex.herokuapp.com/

So cant I use deserialze to read this JSON string?

My Class:

global class AMSAcctsWrper{
    public AMSAcctsWrper()
    {
        AMSAccts = new List<AMSAccts>();
    }

public List<AMSAccts> AMSAccts;

}

public class AMSAccts {
    Public String accid;    
 ....
}

doPut method:

@HttpPut
global static Resp doPut(String req) {
  try{    
     AMSAcctsWrper accParse = (AMSAcctsWrper) System.JSON.deserialize(req, AMSAcctsWrper.class);
     String accid = accParse.AMSAccts[0].accid;
     system.debug ('accid ' + accParse.AMSAccts[0].accid);   

......

My JSON Request:

{
"req":
  [ { 
    "accid":"1256",    
....
    },
   { 
    "accid":"1276",    
....
    }
  ]
} 
1

Because you are manually deserializing you do not need to put the "req" wrapper in your JSON. This is only required when Salesforce is doing the deserializing for you with the method signature so that it knows what top level variables to assign the values to.

Note that you do not necessarily need to deserialize into a wrapper class either: you can deserialize straight into your list of AMSAccts objects. Here is a small sample of what the JSON would look like in the two approaches:

Using Wrapper

JSON

{"AMSAccts": [{"accid": "001"}, {"accid": "002"} ... ]}

APEX

AMSAcctsWrper accParse = (AMSAcctsWrper)System.JSON.deserialize(req, AMSAcctsWrper.class);

Straight To List

JSON

[{"accid": "001"}, {"accid": "002"} ... ]

APEX

List<AMSAccts> accParse = (List<AMSAccts>)System.JSON.deserialize(req, List<AMSAccts>.class);

As you can see, when your JSON is an array you can just start it with an opening brace. The second approach assumes that you move AMSAccts to a top level class. If you left it in the wrapper you would need to reference it as AMSAcctsWrper.AMSAccts.

Hope this helps!

  • Can you be more elaborate? I took off the req from JSON but I get a error "message": "Unexpected character ('[line:2, column:3]". Request looks something like this. { [{ "accid":"1256", ...},{"accid":"1256", ..}]} – Sachin Oct 31 '16 at 15:36
  • I elaborated a bit on my answer with some examples. – dsharrison Oct 31 '16 at 15:45
  • Thanks D.S. . This did not work. I tried both the approaches. The code not even coming to the Deserialization step. Its just failing before that. global static Resp doPut(String req) { system.debug(req); // not printing req try{ AMSAcctsWrper accParse = (AMSAcctsWrper) System.JSON.deserialize(req, AMSAcctsWrper.class); – Sachin Oct 31 '16 at 16:01
  • Ahh, I think I overlooked something in your original post. Can you try using your original JSON but change the method signature of your HttpPut to global static Resp doPut(List<AMSAccts> req)? You specified your parameter as a string so the platform is expecting your JSON to be {"req": "Some string here"}. You are doing extra deserialization by trying to use the built in parameter deserialization then deserializing again in the method. You only need one! If you need to use the deserialization in your method omit the params and use the RestContext class to access the request body. – dsharrison Oct 31 '16 at 23:30
  • global static Resp doPut(List<AMSAccts> req) -- This worked !! Thanks D. S. :) – Sachin Nov 1 '16 at 13:52

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.