I am trying to parse the JSON Response from a Composite REST API call into a wrapper class. However, from the docs, "body" attribute in the response from a Composite Subrequest will be an
Object
in case of a successful request, and will be an
Array
for a failed request.
Successful request:
{
"body" : {
"id" : "001R00000033I6AIAU",
"success" : true,
"errors" : [ ]
},
"httpHeaders" : {
"Location" : "/services/data/v38.0/sobjects/Account/001R00000033I6AIAU"
},
"httpStatusCode" : 201,
"referenceId" : "refAccount"
}
Failed Request:
{
"body" : [ {
"message" : "Email: invalid email address: Not a real email address",
"errorCode" : "INVALID_EMAIL_ADDRESS",
"fields" : [ "Email" ]
} ],
"httpHeaders" : { },
"httpStatusCode" : 400,
"referenceId" : "badContact"
}
How can I parse the response into a Wrapper class if the body attribute in the wrapper is an object or an array?
I am using JSON.deserialize
:
jsonResponseList.add((JSONCompositeRespObj)JSON.deserialize(res.getBody(), JSONCompositeRespObj.class));
JSON.deserializeUntyped(res)
allow you to get the body element & type check from there?