I get a JSON response from external system in the resposne.body()
The JSON looks like this:
"[{\"workOrderNumber\":\"WO-02612\",\"workOrderId\":\"a3F5C000000LHB0UAO\",\"processingStatus\":\"Open\"}]"
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?
Best Answer
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:
Since your JSON starts with square brackets, it's read as a list. Ergo, you should deserialize it as one.
Update
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