Skip to main content

Notifications

Announcements

No record found.

Deserialize JSON collection in X++

Assume Json string response is structured like below.
"data": [
        {
            "SalesOrder": "SO-0001201",
           	"TransDate": "1/1/2024",
           	"status":  { 
                       “currentStatus” : Inprogress,
                       “CreatedDate” : “1/1/2024”
             }
        },
       	{
           	"SalesOrder": "SO-0001202",
           	"TransDate": "12/31/2024",
           	"status": { 
                      “currentStatus” : Inprogress,
                      “CreatedDate” : “12/31/2024”             
            }
        },
        {
           	"SalesOrder": "SO-0001203",
           	"TransDate": "1/10/2024",
           	"status": { 
                       “currentStatus” : Draft,
                       “CreatedDate” : “1/11/2024”
             }

       	}
    ]
To deserialize the response we'll need two contract classes. One class to capture list of all sales order object and one more class to capture the object from status key.

First write the contract class for the Status objects.
[DataContract]
Public class SalesOrderProcessingStatusDC
{
     Str					currentStatus;
     Transdate				orderCreatedDate;

    [DataMemberAttribute(“currentStatus”)]
    Public str parmCurrentStatus(str  _ currentStatus = currentStatus)
    {
	     currentStatus = _ currentStatus;
	     return currentStatus;
    }
    [DataMemberAttribute(“createdDate”)]
    Public Transdate parmCreatedDate(Transdate  _ orderCreatedDate = orderCreatedDate)
    {
	     orderCreatedDate = _ orderCreatedDate;
	     return orderCreatedDate;
    }
}
Write contract class for json collection. Create one method for the status object with SalesOrderProcessingStatusDC as the return type.
[DataContract]
Public class salesorderProcessingDC
{
    Str					salesOrder;
    Transdate				transactionDate;
    SalesOrderProcessingStatusDC		salesOrderProcessingStatusDC;

    [DataMemberAttribute(“SalesOrder”)]
    Public str parmSalesOrder(str _salesOrder = salesOrder)
    {
	    salesOrder = _salesOrder;
	    return salesOrder;
    }

    [DataMemberAttribute(“TransDate”)]
    Public Transdate parmTransDate(Transdate  _ transactionDate = transactionDate)
    {
	    transactionDate = _ transactionDate;
	    return transactionDate;
     }

    [DataMemberAttribute(“status”)]
    Public SalesOrderProcessingStatusDC parmStatus(SalesOrderProcessingStatusDC _ salesOrderProcessingStatusDC  =  salesOrderProcessingStatusDC)
    {
	   salesOrderProcessingStatusDC = _ salesOrderProcessingStatusDC;
	   return salesOrderProcessingStatusDC;
     }
}
To deserialize the JSON
Public void processSalesOrder(str  _jsonResponse)
{
	Str					salesOrderNo, currentStatus;
	TransactionDate				createdDate, lineDate;
	
    List 					salesOrderContractList	= new List(Types::class);
    ListEnumerator				salesOrderContractEnum;

    SalesOrderProcessingDC	salesorderProcessingDC = new SalesOrderProcessingDC();
    SalesOrderProcessingStatusDC	salesOrderStatusContract = new SalesOrderProcessingStatusDC();

    salesOrderContractList		= FormJsonSerializer::deserializeCollectionStrict(classnum(List),
					_ jsonResponse,Types::class,ClassStr(salesorderProcessingDC));

    salesOrderContractEnum	= salesOrderContractList.getEnumerator();

    while(salesOrderContractEnum.moveNext())
    {
		salesorderProcessingDC		= salesOrderContractEnum.current();
	
		salesOrderStatusContract	= salesorderProcessingDC. parmStatus();
		
		salesOrderNo		= salesOrderProcessingDC. parmSalesOrder();
		lineDate		= salesorderProcessingDC. parmTransDate();	
		currentStatus		= salesOrderStatusContract. parmCurrentStatus();
		createdDate		= salesOrderStatusContract. parmCreatedDate();
		
        info(strfmt(“%1, %2, %3, %4”, salesOrderNo, lineDate, currentStatus, createdDate));
     }
}
 

Comments

*This post is locked for comments