0

I have a situation where when I make a callout I get a single json response like this:

{
    "Person": {
        "Infomration": {
            "IndividualInformation": {
                "IndividualCode": "XXXXXX",
                "IndividualType": {
                    "Resident": 0,
                    "Alive": "Yes",
                }
            },FamilyInformation": {
                "FamilyCode": 0101010,
                "PresentInFamily": 1
            }
        },
        ..... a really long json reponse
}

Notice that there is only a single {} indicating a single element. The problem is that sometimes the external system can send more than one person depending of the search result of the external system. I was not given a sample array response, but I am assuming it will be with like this:

[{
        "Person": {
            "Infomration": {
                "IndividualInformation": {
                    "IndividualCode": "XXXXXX",
                    "IndividualType": {
                        "Resident": 0,
                        "Alive": "Yes",
                    }
                },FamilyInformation": {
                    "FamilyCode": 0101010,
                    "PresentInFamily": 1
                }
            },
            ..... 
    }, "Person": {
"Infomration": {
            "IndividualInformation": {
                "IndividualCode": "XXXXXX",
                "IndividualType": {
                    "Resident": 0,
                    "Alive": "Yes",
                }
            },FamilyInformation": {
                "FamilyCode": 0101010,
                "PresentInFamily": 1
            }
        },
        ..... 
}]

I am trying to handle this as there are 2 different reponses. I tried using JSON.deserializeUntyped. But for single reponse I have to use this (Map<String, Object>)JSON.deserializeUntyped(jsonBody);. Obviously doesn't work for array of response. For array I have to use (List<Object>) JSON.deserializeUntyped(jsonBody);. I can't simply cast it (List<Object>) for both array and single.

The only solution I found was:

String respBody = res.getBody();
if(!respBody.left(1).equalsIgnoreCase('[')) respBody = '[' + res.getBody() + ']';

dataFromSystemExternal = (List<ResponseSystemExternal>) JSON.deserialize(respBody, List<ResponseSystemExternal>.Class);

Basically, I am checking the very first char of the respBody to see if it is a '['. If not I am enclosing the respBody between '[' and ']' and deserializing the respBody into a List.

Is there any other solution?

1 Answer 1

0

Personally I use JSON to Apex to deal with dynamic responses. https://json2apex.herokuapp.com/

Paste in the response with multiple customers and then create. It will give you a test class and class to parse the response.

Once you have the parser in Salesforce, the class that is doing the callout can reference the code like this.

Apex Code:

//Parsing the response through the generated class

ParserClassName parsedResponse = ParserClassName .parse(responseBody);

//Looping through the people that are returned

for(ParserClassName.person currentPerson: parsedResponse.response.person){

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .