web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to serialize the json object uisng x++

(0) ShareShare
ReportReport
Posted on by 70

Hi,,

using x++ am trying to send the reference to a third party api

in that process am sending one json file to request and get response from thrid party api

in that json file am not able to separate the header and lines 

"items": [
{
"name": "item_name1",
"itemId": "12345",
"price": 12.97,
"currency": "x",
"quantity": 10,
},{
"name": "item_name2",
"itemId": "12345",
"price": 12.97,
"currency": "x",
"quantity": 10,
},

"orderNumber": "12345",
"reference": "CustomCustomerReference1",//
"address":
{
"postcode": "xx",
"telephone": "x",
"line1": "address line 1",
"country": "x"

}]

i need to separate the header and  lines while creating the json from sale table reference

but when i try to serialize it the json file using datacontract class is not separating the header and lines it coming like this

 [
{
"name": "item_name1",
"itemId": "12345",
"price": 12.97,
"currency": "x",
"quantity": 10,
"name": "item_name2",
"itemId": "12345",
"price": 12.97,
"currency": "x",
"quantity": 10,
"orderNumber": "12345",
"reference": "CustomCustomerReference1",
"name": "First Name Last Name",
"companyName": "Company Name",
"email": "test@mail.com",
"postcode": "xx",
"telephone": "x",
"line1": "address line 1",
"country": "x"

}]

how to separate header and lines 

I have the same question (0)
  • Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: How to serialize the json object uisng x++

    You mentioned a data contract class, but you need two data contracts - one for lines and another for the header, which will also contain a a list of line contracts.

    Please show us your contract class(es) and the code that you use for serialization.

    What you've posted isn't valid JSON. Didn't you mean something like this?

    {
    	"orderNumber": "12345",
    	"reference": "CustomCustomerReference1",
    	"address":
    	{
    		"postcode": "xx",
    		"telephone": "x",
    		"line1": "address line 1",
    		"country": "x"
    	},
    	"items":
    	[
    		{
    			"name": "item_name1",
    			"itemId": "12345",
    			"price": 12.97,
    			"currency": "x",
    			"quantity": 10
    		},
    		{
    			"name": "item_name2",
    			"itemId": "12345",
    			"price": 12.97,
    			"currency": "x",
    			"quantity": 10
    		}
    	]	
    }

    By the way, please use Insert > Insert Code to paste source code, including JSON. That's exactly what I did - doesn't it look much better? It would have even highlighted syntax errors in your code.

  • ram_ind Profile Picture
    70 on at
    RE: How to serialize the json object uisng x++

    yes, i have used two different classes but where i stuck is that

    here 

    object = CLRInterop::getObjectForAnyType(soheader);// here we can pass only one object how can i pass the line data contract object as well
    list.Add(object);

    serializeStr = Newtonsoft.Json.JsonConvert::SerializeObject(list, Newtonsoft.Json.Formatting::Indented);//in

    [DataContractAttribute]
    [Newtonsoft.Json.JsonObject(IsReference = false)]
    [Newtonsoft.Json.JsonArrayAttribute(IsReference = false)]
    class sOHeader
    {
        SalesIdBase                         orderNumber;
        String20                            orderType, reference,employee,channel;    
        Name name;
        CompanyName companyName;
        LogisticsElectronicAddressLocator   email, telephone;
        ItemId                  itemId;
        Price                   price;
        InventQtyAvailPhysical  availPhysical;
        CurrencyCode            currency;
        ItemName                itemName;
        ItemNetWeight           weight; 
        List SOLines,SOAddress;
    
    
        [DataMember("orderNumber")]
        public SalesIdBase parmSalesOrder(SalesIdBase _orderNumber = orderNumber)
        {
            orderNumber = _orderNumber;
            return orderNumber;
        }
    
    
    
        [DataMember("reference")]
        public String20 parmreference(String20 _reference = reference)
        {
            reference = _reference;
            return reference;
        }
    
        [DataMember("name")]
        public Name parmname(Name _name = name)
        {
            name = _name;
            return name;
        }
    
        [DataMember("companyName")]
        public Name parmcompanyName(Name _companyName = companyName)
        {
            companyName = _companyName;
            return companyName;
        }
    
        [DataMember("email")]
        public LogisticsElectronicAddressLocator parmemail(LogisticsElectronicAddressLocator _email = email)
        {
            email = _email;
            return email;
        }
    
        [DataMember("telephone")]
        public LogisticsElectronicAddressLocator parmtelephone(LogisticsElectronicAddressLocator _telephone = telephone)
        {
            telephone = _telephone;
            return telephone;
        }
    
        [DataMember("country")]
        public LogisticsAddressCountryRegionId parmcountry(LogisticsAddressCountryRegionId _country = country)
        {
            country = _country;
            return country;
        }

    above method is that so header contract class

    below so line contract class

    [DataContractAttribute]
    [Newtonsoft.Json.JsonObject(IsReference = false)]
    [Newtonsoft.Json.JsonArrayAttribute(IsReference = false)]
    class SalesLine
    {
        ItemId                  itemId;
        Price                   price;
        InventQtyAvailPhysical  availPhysical;
        CurrencyCode            currency;
        ItemName                itemName;
        ItemNetWeight           weight;
        ItemOrigCountryRegionId country;
    
        [DataMember("itemId")]
        public ItemId parmItemId(ItemId _itemId = itemId)
        {
            itemId = _itemId;
            return itemId;
        }
    
        [DataMember("name")]
        public ItemName parmItemName(ItemName _itemName = itemName)
        {
            itemName = _itemName;
            return itemName;
        }
    
        [DataMember("price")]
        public Price parmPrice(Price _price = price)
        {
            price = _price;
            return price;
        }
    
        [DataMember("currency")]
        public CurrencyCode parmCurrency(CurrencyCode _currency = currency)
        {
            currency = _currency;
            return currency;
        }
    
        [DataMember("quantity")]
        public InventQtyAvailPhysical parmAvailPhysical(InventQtyAvailPhysical _availPhysical = availPhysical)
        {
            availPhysical = _availPhysical;
            return availPhysical;
        }

  • Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: How to serialize the json object uisng x++

    First of all, fix your header contract. Create parm* methods for values that you want to expose (don't forget DataMember and DataCollection attributes) and throw away variables that you aren't using in any parm* method.

    Then you can use FormJsonSerializer class to handle serialization.

  • ram_ind Profile Picture
    70 on at
    RE: How to serialize the json object uisng x++

    [DataContractAttribute]
    [Newtonsoft.Json.JsonObject(IsReference = false)]
    [Newtonsoft.Json.JsonArrayAttribute(IsReference = false)]
    class sOHeader
    {
        SalesIdBase                         orderNumber;
        String20                            orderType, reference,employee,channel;    
        Name name;
        CompanyName companyName;
        LogisticsElectronicAddressLocator   email, telephone;
        ItemId                  itemId;
        Price                   price;
        InventQtyAvailPhysical  availPhysical;
        CurrencyCode            currency;
        ItemName                itemName;
        ItemNetWeight           weight; 
        List SOLines,SOAddress;
    
    
        [DataMember("orderNumber")]
        public SalesIdBase parmSalesOrder(SalesIdBase _orderNumber = orderNumber)
        {
            orderNumber = _orderNumber;
            return orderNumber;
        }
    
    
    
        [DataMember("reference")]
        public String20 parmreference(String20 _reference = reference)
        {
            reference = _reference;
            return reference;
        }
    
        [DataMember("name")]
        public Name parmname(Name _name = name)
        {
            name = _name;
            return name;
        }
    
        [DataMember("companyName")]
        public Name parmcompanyName(Name _companyName = companyName)
        {
            companyName = _companyName;
            return companyName;
        }
    
        [DataMember("email")]
        public LogisticsElectronicAddressLocator parmemail(LogisticsElectronicAddressLocator _email = email)
        {
            email = _email;
            return email;
        }
    
        [DataMember("telephone")]
        public LogisticsElectronicAddressLocator parmtelephone(LogisticsElectronicAddressLocator _telephone = telephone)
        {
            telephone = _telephone;
            return telephone;
        }
    
        [DataMember("country")]
        public LogisticsAddressCountryRegionId parmcountry(LogisticsAddressCountryRegionId _country = country)
        {
            country = _country;
            return country;
        }
        
            [DataMemberAttribute('Lines'), DataCollectionAttribute(Types::Class, classStr(SalesLine))]
        public List parmSOLines(List _SOLines = SOLines)
        {
            SOLines = _SOLines;
            return SOLines;
        }
        
        sample code to insert data into these methods 
        soheader contract class
        soheader.parmSalesOrder(salestable.SalesId);
                soheader.parmChannel(salestable.SalesPoolId);
                soheader.parmreference(salestable.CustomerRef);
                
        sales line
        SalesLine.parmItemId(salesline.ItemId);
                    SalesLine.parmItemName(salesline.itemName());
                    SalesLine.parmAvailPhysical(salesline.QtyOrdered);
                    SalesLine.parmPrice(salesline.LineAmount);
                    
        how to combine these two data contract class in a single json file

    in the above i have created object list for sales line

    and also i have the given sample that i used to insert values into the contract methods 

    we will use different variable objects for header and lines 

    but when we serialize it we will give one object then only header or lines json we can get not the header and lines both

  • ram_ind Profile Picture
    70 on at
    RE: How to serialize the json object uisng x++

    yes am using parm methods with data member attribute only

  • ram_ind Profile Picture
    70 on at
    RE: How to serialize the json object uisng x++

    can anyone tell me how to combine the two different contract class while serializing

  • ergun sahin Profile Picture
    8,826 Moderator on at
    RE: How to serialize the json object uisng x++

    www.linkedin.com/.../

    allaboutdynamic.com/.../

  • Verified answer
    Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: How to serialize the json object uisng x++

    What do you mean by "how to combine the two different contract class while serializing". Are you saying that you don't know how to add an object to a list?

    Let me simplify the contract for demo purposes (I've also added a company prefix - Xyz - and a 'contract' suffix to your classes, because that's how the names should look like).

    [DataContract]
    class XyzSalesLineContract
    {
        ...
    }
    
    [DataContract]
    class XyzSalesOrderContract
    {
        // I'm creating a new List here, so I don't have to check for null later
    	List lines = new List(Types::Class);
    	
    	[
    		DataMember('Lines'),
    		DataCollection(Types::Class, classStr(XyzSalesLineContract))
    	]
        public List parmLines(List _lines = lines)
        {
            lines = _lines;
            return lines;
        }
    }

    Then you can create objects and set a line to a header with this code:

    XyzSalesOrderContract header = new SalesOrderContract();
    XyzSalesLineContract line = new XyzSalesLineContract();
    header.parmLines().addEnd(line);

  • ram_ind Profile Picture
    70 on at
    RE: How to serialize the json object uisng x++

    yes i dont know how to add object to a list

    thanks for your code it worked for me

  • Ravi Kiran Profile Picture
    50 on at
    RE: How to serialize the json object uisng x++

    Hi ram_ind,

    Can you please let me know how you are sending the data to third party?

    are you using the Logic apps?

    I have the similar requirement can you please share on the steps how you have followed, is it possible to share the final code piece on how you generated the json body.

    Thanks in Advance

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans