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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

XML to JSON and deserializing

(0) ShareShare
ReportReport
Posted on by 260

Hello guys,

I have been working on a web service, I did everything based on json, and everything was going well, but the client wants to send data in XML.

So, I was planning to convert XML to JSON on the web client, in that way I didn't need to change the code on my web service. 

However, when I convert XML to JSON sometimes it ignore that fact that Items or Boxes are arrays. 
If I have 2 container on the XML it will identify that it is an array, but if I have one it will send as an unique object.

Like in the example below. The first box has 2 items so it did convert correctly, but the second box has only one, and it return without [ .

When I receive these values on the web service it will miss these values when I do the deserialization because my contract class look like this

    [
        DataMemberAttribute('Items'),
        DataCollectionAttribute(Types::Class, classStr(ItemsContract))
    ]
    public List parmItems(List _items = items)
    {
        items = _items;
        return items;
    }

{
  "Shipment": {
    "ShipmentId": "Value",
    "Boxes": [
      {
        "BoxID": "001",
        "Weight": "1",
        "Items": [
          {
            "ItemId": "WHSSerialized",
            "Qty": "1"
          },
          {
            "ItemId": "WHSSerialized",
            "Qty": "1"
          }
        ]
      },
      {
        "BoxID": "002",
        "Weight": "1",
        "Items": {
          "ItemId": "WHSSerialized",
          "Qty": "1"
        }
      }
    ]
  }
}

Any ideas? 

Thanks

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

    Hi bois0155,

    Could you, please, share details about how do you convert XML to JSON on the web client? What is the web client in this case?

  • bois0155 Profile Picture
    260 on at

    Hello Sergei,

    I created a basic asp.net page

    I'm using

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(xmlString);

    string json = JsonConvert.SerializeXmlNode(doc);

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi bois0155,

    So it's a JSON.NET library.

    There are a lot of articles on how to force array in XML->JSON conversion

    One of them stackoverflow.com/.../force-json-net-to-convert-xml-to-json-array

  • bois0155 Profile Picture
    260 on at

    Nice, thanks I'll try it.

    Taking advantage of the post let me ask another thing. The best practice is to send a json string to the web service and deserialize there, or send the object directly?

    Right now, I'm sending a string, but it looks weird when I test using postman I have to do something like the photo below to work.

    My method is receiving a string paremeter public void process(str _request) ...

    pastedimage1604951712340v1.png

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi bois0155,

    I would say the best practice is to use the contract as a parameter for the web service method, instead of a string parameter. It doesn't make too much difference for JSON-based service, but there is also SOAP-based service is available and wsdl schema will be generated based on contracts for web service.

    docs.microsoft.com/.../custom-services

  • bois0155 Profile Picture
    260 on at

    Hey Sergei,

    I did as you suggested, which means that I don't need to deserialize anymore. I'm using the contract as a parameter. However, I'm dealing with a nested json and I have 3 contract class, I can get the values from the RootContract, but when it try to get the boxes it return the list as a <JObject> instead of my BoxContract class.

    ListEnumerator          enumerator              = RootContract.parmBoxes().getEnumerator();

    pastedimage1604956886996v2.png

    If I send the parameter as string, and deserialize it, it works perfectly, but when I try to use the Contract is parameter don't.

    I have  this inside RootContract

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

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi bois0155,

    For web service contracts you need to use AifCollectionTypeAttribute (parameter and return type) for collection member.

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 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans