1

I am writing a REST Service and have created Request and Response Wrappers, Finding it difficult to generate the JSON Request Body out of the wrapper classes. Below are the Sample Classes.

public class requestResource{
    public orderRequestResource order {get;set;}
}
public class orderRequestResource {        
    public String                                 accountUNumber             {get;set;}
    public String                                 contractNumber             {get;set;}
    public string                                 status                    {get;set;}        
    public string                                 externalKey                {get;set;}        
    public string                                 orderStartDate            {get;set;}        
    public string                                 priceBookName            {get;set;}        
    public List<orderLineItemRequestResource>     orderItems                 {get;set;}        
}
public class orderLineItemRequestResource{    
    public string    quantity            {get;set;}    
    public string    unitPrice           {get;set;}
    public string    description         {get;set;}
    public string    productCode         {get;set;}    
    public string    externalKey         {get;set;}
}    
2

When we create JSON of wrapper class we first create different child objects which are required to generate final object so for your wrapper class you need object of orderRequestResource class which needs first List of orderLineItemRequestResource object.

So it will go in following way.

//create lineItemRequest
List<orderLineItemRequestResource> linteItemList = new List<orderLineItemRequestResource>();
orderLineItemRequestResource linteItem = new orderLineItemRequestResource();
lineItem.quantity = 'xxx';
lineItem.unitPrice = 'xxx';
lineItem.description = 'xxx';
lineItem.productCode = 'xxx';
lineItem.externalKey = 'xxx';
linteItemList.add(lineItem);


//order request resource object
orderRequestResource objOrderRequest = new orderRequestResource();
objOrderRequest.accountUNumber = '';
objOrderRequest.contractNumber = '';
objOrderRequest.status = '';
objOrderRequest.externalKey = '';
objOrderRequest.orderStartDate = '';
objOrderRequest.priceBookName = '';
/********because it is expecting a list of*********************/
/********orderLineItemRequestResource we will first create******/
list before assigning here.
objOrderRequest.orderItems = linteItemList;


//Final object which is required for call out
requestResource rest = new requestResource();
rest.order = objOrderRequest;
// Convert to JSON format
String finalJSON = System.json.serialize(rest);
0

Code like this will construct and assemble the objects so they can then be serialized:

orderLineItemRequestResource oi = new orderLineItemRequestResource();
oi.quantity = '2';
...

orderRequestResource o = new orderRequestResource();
o.accountUNumber = '123456';
...
o.orderItems = new List<orderLineItemRequestResource>();
o.orderItems.add(oi);

requestResource r = new requestResource();
r.order = o;

String jsonString = JSON.serialize(r);

However, presumably you want to populate the fields with e.g. queried data and that would be more convenient if you added constructors and other methods to the classes. It would also make sense to use more specific data types for the fields as JSON supports numbers and true/false as well as strings.

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.