1

I am trying to parse a nested JSON and have generated the apex code using JSON2Apex as below:

global with sharing Class ServiceUpdates 
    {
      @HttpPost
        global static void updateservice()
        { 
            RestRequest r = RestContext.request;
            String jsonRequest = r.requestBody.toString();

                   JSON2Apex wscall = (JSON2Apex)JSON.deserialize(jsonRequest, JSON2Apex.class);
             or    ServiceUpdates.JSON2Apex wscall = (ServiceUpdates.JSON2Apex)JSON.deserialize(jsonRequest, ServiceUpdates.JSON2Apex.class);
             or    ServiceUpdates wscall = (ServiceUpdates)JSON.deserialize(jsonRequest, ServiceUpdates.class); 
            System.debug('response' +wscall );

        }


    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 Attribute attribute;
        }

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

    }
 }

I'm getting error that Inner types are not allowed to have inner types If I remove the outer-most class from auto generated code and add in existing class then I get NULL in inner object in parsing. Can someone please help how to structure code to parse the json?

UPDATED JSON DETAILS:

{
    "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"
                                }
                            }
                        ],
                        "Service": [
                            {
                                "attribute": {
                                    "name": "Resilience",
                                    "value": "Terminated"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "Number",
                                    "value": "1234"
                                }
                            },
                            {
                                "attribute": {
                                    "name": "serviceStartDate",
                                    "value": "2018-08-220T02:13:20"
                                }
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

2 Answers 2

2

JSON2Apex presumes that the class will be its own top-level class. If you wish to place that class inside another, you also have to move all inner classes from JSON2Apex outside of it. Here's what that looks like:

global with sharing Class ServiceUpdates 
    {
      @HttpPost
        global static void updateservice()
        { 
            RestRequest r = RestContext.request;
            String jsonRequest = r.requestBody.toString();

                   JSON2Apex wscall = (JSON2Apex)JSON.deserialize(jsonRequest, JSON2Apex.class);
             or    ServiceUpdates.JSON2Apex wscall = (ServiceUpdates.JSON2Apex)JSON.deserialize(jsonRequest, ServiceUpdates.JSON2Apex.class);
             or    ServiceUpdates wscall = (ServiceUpdates)JSON.deserialize(jsonRequest, ServiceUpdates.class); 
            System.debug('response' +wscall );

        }

        public class Subscription {
            public String SAPOrder;
            public String RequestType;
            public String OrderStatus;
            public List<Services> Services;
        }
    public class Services {
            public List<Service> Service;
        }
        public class Attribute {
            public String name;
            public String value;
        }

        public class Subscriptions {
            public Subscription Subscription;
        }
        public class Service {
            public Attribute attribute;
        }

    public class JSON2Apex 
    {
        public List<Subscriptions> Subscriptions;
    }
 }
1
  • Thanks sfdcfox! This answers my class issue but despite of having services in list this json fails when I post the request. 'System.JSONException: Duplicate field: ' It accepts repeats for attribute but not for service. I have updated the json in the question, please if you can help. Thanks Sep 1, 2018 at 11:08
3

You are getting this error because in its current form, you have 2 level deep of inner class which are not allowed in Apex. Refer to the excerpt from the documentation:

You can only have inner classes one level deep.

In its current form, your class is as:

class ServiceUpdates {
    class JSON2Apex { // level one
        class Services { // level two -- not allowed!
        }
    }
}

You should remove JSON2Apex class from ServiceUpdates and have it as separate class, that should resolve this issue. Something as below:

ServiceUpdates.apxc:

class ServiceUpdates {
}

JSON2Apex.apxc:

class JSON2Apex {
    class Services { // just one level deep now
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.