Skip to main content
added 339 characters in body
Source Link
Jayant Das
  • 30.7k
  • 3
  • 42
  • 68

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

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.

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);
Source Link
Jayant Das
  • 30.7k
  • 3
  • 42
  • 68

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.