Skip to main content
added 804 characters in body
Source Link
shukla yogesh
  • 398
  • 2
  • 6
  • 22

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);
    }
}

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);
    }
}
added 1487 characters in body
Source Link
shukla yogesh
  • 398
  • 2
  • 6
  • 22

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"
                }
            }

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"
                }
            }
Source Link
shukla yogesh
  • 398
  • 2
  • 6
  • 22

How to handle multiple type of json in one parser class?

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.