0

I am expecting two separate jsons in case of success and failure.

In case of success I get a nested json whereas in failure I get a simple json mentioning status : failed so my class I used to handle success is failing for failure scenario. Can someone please guide here? Only way I can think in checking the substring of json and validate before parsing but that doesn't seem a good solution.

Updated Jsons: Success:

{
    "Subscriptions": [
        {
            "Subscription": {
                "SAPOrder": "SAP123",
                "RequestType": "New",
                "OrderStatus": "0",
                "Services": [
                    {
                        "Service": [{
                            {
                                "attribute": {
                                    "name": "Resilience",
                                    "value": "Terminated"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "Number",
                                    "value": "1234"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "serviceStartDate",
                                    "value": "2018-08-220T02:13:20"
                                }
                            }
                        },
                            {{
                                "attribute": {
                                    "name": "Resilience",
                                    "value": "Terminated"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "Number",
                                    "value": "1234"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "serviceStartDate",
                                    "value": "2018-08-220T02:13:20"
                                }
                            }
                        }
                        ]
                    }
                ]
            }
        }
    ]
}

Failure:

  {
        "Subscriptions": [
            {
                "Subscription": {
                    "SAPOrder": "SAP123",
                    "RequestType": "New",
                    "OrderStatus": "Failed"
                }
            }

Added Class details:

public class JSON2Apex {

    public class Services {
        public List<Service> Service;
    }

    public List<Subscriptions> Subscriptions;

    public class Attribute {
        public String name;
        public String value;
    }

    public class Subscriptions {
        public Subscription Subscription;
    }

    public class Service {
        public List<Attribute> attribute;
    }

    public class Subscription {
        public String SAPOrder;
        public String RequestType;
        public String OrderStatus;
        public List<Services> Services;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}
2
  • can you post apex class as well? Commented Sep 17, 2018 at 12:09
  • I have added the class details.. Commented Sep 18, 2018 at 11:55

2 Answers 2

3

You don't need to do anything extra here. As I see in your responses, you still have all attributes in the error JSON except for the array of Services, which you get only in success.

"Subscription": {
       "SAPOrder": "SAP123",
       "RequestType": "New",
       "OrderStatus": "0",
}

So as long as your wrapper class is in the below format:

public class SubscriptionWrapper {
    public Subscription subscription;
    ....
}

public class Subscription {
    public String SAPOrder;
    public String RequestType;
    public String OrderStatus;
    public List<Services> services
}

And that you receive the error vs. success on the OrderStatus variable, while deserializing your response, you get only what's in the JSON. If the List<Services> is present in the JSON in case of success, you get that too when you deserialize, else not.

And then you use the OrderStatus value to determine if you received a success vs. error message and use it accordingly as required.

As an example, the below code will work without any issue whether or not you have all the attribtues in the JSON message or not.

String jsonStr = '{\"OrderStatus\": \"Success\"}';

Subscription subsc = (Subscription)JSON.deserialize(jsonStr, Subscription.class);
system.debug('Order Status:' + subsc.OrderStatus);
10
  • Yes, that's my problem. I can't see orderstatus because parsing will fail due to list<services> not found. Any alternative for that ? Commented Sep 17, 2018 at 13:28
  • Have you tried parsing already? I can tell you if the JSON does not contain an attribute in the message, and that when you deserialize the response, your List<Services> will just be initialized with null.
    – Jayant Das
    Commented Sep 17, 2018 at 13:29
  • And I added a sample "working example". So you should try the parsing first -- it won't fail.
    – Jayant Das
    Commented Sep 17, 2018 at 13:36
  • 1
    You should review your implementation and the JSON to make sure it aligns to your wrapper class. Because you don't have the wrapper class in here, and the one I posted in the example works perfectly fine without the attribute being present in the JSON.
    – Jayant Das
    Commented Sep 18, 2018 at 12:57
  • 1
    The issue is with your JSON. If Services is supposed to be an array of values, then if at all it is present in the JSON, then it should be sent as Services : [] and not as null. However in your question, it seemed like that array never is sent in the JSON. But in any case, as long as your JSON sends the data based on its schema, your parsing will absolutely work fine.
    – Jayant Das
    Commented Sep 18, 2018 at 13:36
0

you can create virtual class to have status of your request, and after that you can extend this class with another one containing success answer fields. E.g.

public virtual class BaseResponse {
    public String status {get; set;}
    public String error {get; set;}
}

public class YourResponse extends BaseResponse {
    public List<String> yourDate {get; set;}
}

after that you can parse response in the following way:

YourResponse response = (YourResponse) JSON.deserialize(body, YourResponse .class);
if(response.status == 'success'){
    // do actions on success
} else if(response.status == 'failed'){
    // whos error message e.g. response.error
}

this virual class allows you to create other classes, that also have this success-fail responses

1
  • Thanks Alexander. I have updated the question with jsons. My class fails in case of second json. Commented Sep 17, 2018 at 12:04

You must log in to answer this question.

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