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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Deserialize Json with multiple contract

(0) ShareShare
ReportReport
Posted on by 260

Hello Guys, 

I'm working in a project that I have a json string, and I want to deserialize it. I did a lot o research, but I couldn't find what I need.
Right now, I can deserialize only simple json, but when it is a nested json I don't know how to proceed. 

I'm using this function to deserialize -> FormJsonSerializer::deserializeObject(classNum(NmbPwbContract), jsonString);

After some research this is how far I got. 

Json:

{
   "ShipmentId": "ShipmentValue",
   "Boxes": [
      {
         "BoxId": "Value",
         "Weight": "Value",
         "Items": [
            {
               "ItemId": "Value",
               "Qty": "1"
            },
            {
               "ItemId": "Value",
               "Qty": "1"
            }
         ]
      },
      {
         "BoxId": "Value",
         "Weight": "Value",
         "Items": {
            "ItemId": "Value",
            "Qty": "1"
         }
      }
   ]
}

RootContract

[DataContractAttribute]
class RootContract
{
    Shipment shipmentId
    List boxes = new List(Types::Class);

    [DataMemberAttribute('ShipmentId')]
    public Shipment parmShipmentId(Shipment _shipmentId = shipmentId)
    {
        shipmentId = _shipmentId;
        return shipmentId;
    }

    [
        DataMemberAttribute('Boxes'),
        AifCollectionTypeAttribute('_boxes', Types::Class, classStr(BoxesContract)),
        AifCollectionTypeAttribute('return', Types::Class, classStr(BoxesContract))
    ]
    public List parmBoxes(List _boxes = boxes)
    {
        boxes = _boxes;
        return boxes;
    }
}

BoxesContract

[DataContractAttribute]
class BoxesContract
{
    str     boxId
    Weight  weight;
    
    [DataMemberAttribute('BoxId')]
    public str parmBoxId(str _boxId = boxId)
    {
        boxId = _boxId;
        return _boxId;
    }

    [DataMemberAttribute('Weight')]
    public real parmWeight(real _weight = weight)
    {
        weight = _weight;
        return _weight;
    }

}

I can retrieve the shipmentid information, but the boxes return empty. 

Any help? Thanks

I have the same question (0)
  • Verified answer
    Sergei Minozhenko Profile Picture
    23,097 on at

    Hi bois0155,

    You need to use DataCollectionAttribute for Boxes member in RootContract

    [DataContractAttribute]
    class RootContract
    {
        str shipmentId;
        List boxes = new List(Types::Class);
    
        [DataMemberAttribute('ShipmentId')]
        public str parmShipmentId(str _shipmentId = shipmentId)
        {
            shipmentId = _shipmentId;
            return shipmentId;
        }
    
        [
            DataMemberAttribute('Boxes'),
            DataCollectionAttribute(Types::Class, classStr(BoxesContract))
        ]
        public List parmBoxes(List _boxes = boxes)
        {
            boxes = _boxes;
            return boxes;
        }
    
    }

  • bois0155 Profile Picture
    260 on at

    Hello Sergei,

    Thank you for the reply.

    After the suggested changes it is partially working now, after deserialize I did serialize it back just to check the output, and it is getting the information only for the first box, the second one is there, but return empty strings on the values.

    Output json string:

    "{\"Boxes\":[{\"BoxId\":\"Value\",\"Weight\":1.0},{\"BoxId\":\"\",\"Weight\":0.0}],\"ShipmentId\":\"ShipmentValue\"}"

    Also, I tried to retrieve the information like below, but it doesn't work I don't even have the options to select the methods from BoxesContract after .current()

    RootContract.parmBoxes().getEnumerator().current().parmBoxId();

    Error message: 

    An exception of type 'Microsoft.Dynamics.Ax.Xpp.MethodNotFoundException' occurred in aoskernel.dll but was not handled in user code

    Additional information: Error executing code: <empty class> object does not have method 'parmBoxId'

    Any idea about these 2 issues??

  • Verified answer
    Martin Dráb Profile Picture
    240,045 Most Valuable Professional on at

    You test two many things at once. To know if deserialization works or not, test deserialization only. Simply assign the result of deserializeObject() to a variable, run the code in debugger and check if the variable contains expected data.

    Your second code snippet can't work, because you're not using the enumerator correctly. You would need something like this:

    ListEnumerator enumerator = rootContract.parmBoxes().getEnumerator();
    while (enumerator.moveNext())
    {
    	BoxContract box = enumerator.current();
    	box.parmBoxId();
    }

  • bois0155 Profile Picture
    260 on at

    Hello Martin,

    Thank you for the reply, I did what you said, and the values are being populated correctly I can see them on debug mode,

    and I managed to retrieve the values using ListEnumerator like you said.

    Looks like everything is working now thanks.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 412 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 396

#3
Martin Dráb Profile Picture

Martin Dráb 261 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans