I get a JSON response from external system in the resposne.body()

The JSON looks like this:


I created a class in my org to deserialize the json.

public class ResponseJSON{

public String sampleNumber;
public String SampleID;
public String state;


I did get an error when deserializing the json saying it's a Malformed JSOn because '{' is missing, so it wasn't the correct JSON format. So added the '{' and '}' in the beginning and end of JSON when deserializing the JSON.

ResponseJSON respJson =(ResponseJSON)JSON.deserialize('{' + response.getBody() + '}', ResponseJSON.class);

But this gives me an error:

"common.apex.runtime.impl.ExecutionException: Unexpected character ('}' (code 125)): was expecting a colon to separate field name and value at {the above line}

What am I missing here?

  • Your JSON is an array of response class, so please try next instead JSON.deserialize(...,List<ResponseJSON>.class); – kurunve Apr 11 '17 at 20:09
  • @kurunve making it as a List I now get an error - "common.apex.runtime.impl.ExecutionException: Malformed JSON: Expected '[' at the beginning of List/Set. Any idea? – SfdcBat Apr 11 '17 at 20:42

Once you add the curly brackets, your JSON looks like this


Which is invalid JSON because you have an list inside of an object without a property name. Instead of adding the curly brackets, change your code to deserialize the JSON differently:

List<ResponseJSON> respJson =(List<ResponseJSON>)JSON.deserialize(response.getBody(), List<ResponseJSON>.class);

Since your JSON starts with square brackets, it's read as a list. Ergo, you should deserialize it as one.


Since you mention in the comments that the external system is adding quotes to your code, I suspect you can do this to make the string JSON-friendly before deserializing

String jsonString = (String)JSON.deserialize(response.getBody(), String.class); 

List<ResponseJSON> respJson =(List<ResponseJSON>)JSON.deserialize(jsonString, List<ResponseJSON>.class);
| improve this answer | |
  • This is a list, serialized into JSON... creating a string... which has then been serialized to JSON again (hence the backslashed double-quotes.) – Michael - sqlbot Apr 11 '17 at 20:59
  • 1
    I agree @SebastianKessel. +1, the safest thing, if this is what the external system does, would be to deserialize it into a string, and then deserialize that result into a list. – Michael - sqlbot Apr 11 '17 at 21:14

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.