How to apply XSL Transform in Azure APIM which will convert single value element to array .

prajakta nawale 1 Reputation point
2021-12-15T12:06:49.163+00:00

Hello Team,

I have requirement where we need to convert XML to json in Azure API in inbound policy.
And XML contains some element for which sometimes value is single element.
However, we want that while converting to JOSN it should be array and not object.
We have tried to doing this with c# code as below to force array and result is element value contains object which is not expected.
We will need element value as an array.
Please do let us know how to achieve this in APIM.

var tagNames = new List<string> {"Addresses";
foreach (var tagName in tagNames)
{
var aoNodes = doc.GetElementsByTagName(tagName);
foreach (XmlElement n in aoNodes)
{
var attribute = doc.CreateAttribute("json", "Array", "http://james.newtonking.com/projects/json");
attribute.InnerText = "true";
n.Attributes.Append(attribute);
}
}
return JsonConvert.SerializeObject(doc);

Sample Example:
<?xml version="1.0" encoding="utf-8"?>
<sh xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Addresses>
<Address>

  <Name1>tst</Name1>
  <Name2>abs</Name2>
  <Street1> 38</Street1>
  <Street2>tes</Street2>
  <PostCode>4180</PostCode>
  <City>asd</City>
  <Phone>11111</Phone>
  <Attention>Rcdsd</Attention>

</Address>

</Addresses>
</sh>

Actual Result
"Sh": {
"Addresses": [
{
"Address": [
{
"Name1": "tst",
"Name2": "abs",
"Street1": " 38",
"Street2": "tes",
"PostCode": "4180",
"City": "ass",
"Phone": "11",
"Attention": "asa",

      }
    ]
  }
],

}

Expected Result:
"Sh": {
"Addresses":[

{

        "Name1": "tst",
        "Name2": "abs",
        "Street1": " 38",
        "Street2": "tes",
        "PostCode": "4180",
        "City": "ass",
        "Phone": "11",
        "Attention": "asa",

      }

]
}

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,652 questions
{count} votes