Xml To Json

Provides the ability to map XML source data to a specific JSON destination.

This is all done via looping over the desired XML elements and retrieving the source data via the use of XPath selector statements.

Version 1

HTTP Request
POST /ado/v1/XmlToJson

Header

ParameterDescription
Ocp-Apim-Subscription-KeyThe subscription key you received when you purchased a plan.

Request Body

Mandatory

ParameterTypeDescription
primaryLoopAtElementstringThis is the primary XPath selector that will form the basis of the array that will be returned in the results.
datastringEncoded XML string which represents the data that needs to be processed.
mapobjectThis is the JSON object that will contain a set of properties mapped via the use of XPath statements.

Optional

Sub Loops (subLoopAtElements[])

Sub looping provides the ability to extract different levels of array data present in the XML so it can be represented in a logical way as it was originally (i.e. header and item concept).

ParameterTypeDescription
mapNamestringThe name of the map. This map name should be specified in other sub loops or on the primary loop section of the payload. It is recommended to begin each mapName with a $ sign. See example.
xPathQuerystringThis is the XPath statement that is used to select all of the underlying items to process. This is much like the primaryLoopAtXPathElement only it’s specific to sub loops.
mapobjectThis is the JSON object that will contain a set of properties mapped via the use of XPath statements.

Settings

ParameterTypeDescription
convertAnythingThatLooksLikeANumberToANumberbooleanThis is the primary XPath selector that will form the basis of the array that will be returned in the results.
setEmptyValuesToNullbooleanIf an XPath query returns no data for a given property, you can set this variable to true if you want to set the resulting value to null. If set to false, the value returned will be a blank string.
errorOnMultipleNodeQueryResultbooleanIf an XPath query on a map property returns multiple results, you can choose to throw an exception or continue on with the process.
multipleNodeQueryResultSelectionTypestringIf the errorOnMultipleNodeQueryResult setting is set to false, you must choose what you want to return when more than one element is found from the XPath selection. Must be one of the following values … FirstLast, or All

This setting is holistic across the entire execution. If you need a different setting for different properties, you will need to use the relevant XPath statements to do that for you, i.e. Last(), [1], etc.

Limitations

Using the built in ado: functions can only be applied to XPath statements that traverse down the XML hierarchy. If the main loop or a sub loop tries to retrieve the value of an ancestor node or an attribute of an ancestor node, it will return a null/empty value.

Example XML

For the purpose of each example, the following XML has been encoded and passed into the data property.

This sample was taken from the following URL: https://learn.microsoft.com/en-US/dotnet/standard/linq/sample-xml-file-multiple-purchase-orders-namespace

<?xml version="1.0" encoding="utf-8"?>
<aw:PurchaseOrders xmlns:aw="http://www.adventure-works.com">
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99503" aw:OrderDate="1999-10-20">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Ellen Adams</aw:Name>
            <aw:Street>123 Maple Street</aw:Street>
            <aw:City>Mill Valley</aw:City>
            <aw:State>CA</aw:State>
            <aw:Zip>10999</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Tai Yee</aw:Name>
            <aw:Street>8 Oak Avenue</aw:Street>
            <aw:City>Old Town</aw:City>
            <aw:State>PA</aw:State>
            <aw:Zip>95819</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:DeliveryNotes>Please leave packages in shed by driveway.</aw:DeliveryNotes>
        <aw:Items>
            <aw:Item aw:PartNumber="872-AA">
                <aw:ProductName>Lawnmower</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>148.95</aw:USPrice>
                <aw:Comment>Confirm this is electric</aw:Comment>
            </aw:Item>
            <aw:Item aw:PartNumber="926-AA">
                <aw:ProductName>Baby Monitor</aw:ProductName>
                <aw:Quantity>2</aw:Quantity>
                <aw:USPrice>39.98</aw:USPrice>
                <aw:ShipDate>1999-05-21</aw:ShipDate>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99505" aw:OrderDate="1999-10-22">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Cristian Osorio</aw:Name>
            <aw:Street>456 Main Street</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Cristian Osorio</aw:Name>
            <aw:Street>456 Main Street</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:DeliveryNotes>Please notify me before shipping.</aw:DeliveryNotes>
        <aw:Items>
            <aw:Item aw:PartNumber="456-NM">
                <aw:ProductName>Power Supply</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>45.99</aw:USPrice>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
    <aw:PurchaseOrder aw:PurchaseOrderNumber="99504" aw:OrderDate="1999-10-22">
        <aw:Address aw:Type="Shipping">
            <aw:Name>Jessica Arnold</aw:Name>
            <aw:Street>4055 Madison Ave</aw:Street>
            <aw:City>Seattle</aw:City>
            <aw:State>WA</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Address aw:Type="Billing">
            <aw:Name>Jessica Arnold</aw:Name>
            <aw:Street>4055 Madison Ave</aw:Street>
            <aw:City>Buffalo</aw:City>
            <aw:State>NY</aw:State>
            <aw:Zip>98112</aw:Zip>
            <aw:Country>USA</aw:Country>
        </aw:Address>
        <aw:Items>
            <aw:Item aw:PartNumber="898-AZ">
                <aw:ProductName>Computer Keyboard</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>29.99</aw:USPrice>
            </aw:Item>
            <aw:Item aw:PartNumber="898-AM">
                <aw:ProductName>Wireless Mouse</aw:ProductName>
                <aw:Quantity>1</aw:Quantity>
                <aw:USPrice>14.99</aw:USPrice>
            </aw:Item>
        </aw:Items>
    </aw:PurchaseOrder>
</aw:PurchaseOrders>
Code language: HTML, XML (xml)

Examples

Simple
Sub Loops
Advanced
Direct Conversion
Flatten

The sample XML contains a nested structure containing multiple levels of data.

This sample takes the top level data and transforms the XML into a basic three property object per purchase order.

The settings are set as defaults, therefore, anything that looks like a number is formatted as such and values which are undeterminable are returned as null.

Request

{
    "primaryLoopAtElement": "//aw:PurchaseOrder",
    "map": {
        "purchaseOrderNumber": "@aw:PurchaseOrderNumber",
        "orderDate": "@aw:OrderDate",
        "deliveryNotes": "aw:DeliveryNotes"
    },
    "data": "<?xml version=\"1.0\" encoding=\"utf-8\"?><aw:PurchaseOrders xmlns:aw=\"http://www.adventure-works.com\"><aw:PurchaseOrder aw:PurchaseOrderNumber=\"99503\" aw:OrderDate=\"1999-10-20\"><aw:Address aw:Type=\"Shipping\"><aw:Name>Ellen Adams</aw:Name><aw:Street>123 Maple Street</aw:Street><aw:City>Mill Valley</aw:City><aw:State>CA</aw:State><aw:Zip>10999</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:Address aw:Type=\"Billing\"><aw:Name>Tai Yee</aw:Name><aw:Street>8 Oak Avenue</aw:Street><aw:City>Old Town</aw:City><aw:State>PA</aw:State><aw:Zip>95819</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:DeliveryNotes>Please leave packages in shed by driveway.</aw:DeliveryNotes><aw:Items><aw:Item aw:PartNumber=\"872-AA\"><aw:ProductName>Lawnmower</aw:ProductName><aw:Quantity>1</aw:Quantity><aw:USPrice>148.95</aw:USPrice><aw:Comment>Confirm this is electric</aw:Comment></aw:Item><aw:Item aw:PartNumber=\"926-AA\"><aw:ProductName>Baby Monitor</aw:ProductName><aw:Quantity>2</aw:Quantity><aw:USPrice>39.98</aw:USPrice><aw:ShipDate>1999-05-21</aw:ShipDate></aw:Item></aw:Items></aw:PurchaseOrder><aw:PurchaseOrder aw:PurchaseOrderNumber=\"99505\" aw:OrderDate=\"1999-10-22\"><aw:Address aw:Type=\"Shipping\"><aw:Name>Cristian Osorio</aw:Name><aw:Street>456 Main Street</aw:Street><aw:City>Buffalo</aw:City><aw:State>NY</aw:State><aw:Zip>98112</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:Address aw:Type=\"Billing\"><aw:Name>Cristian Osorio</aw:Name><aw:Street>456 Main Street</aw:Street><aw:City>Buffalo</aw:City><aw:State>NY</aw:State><aw:Zip>98112</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:DeliveryNotes>Please notify me before shipping.</aw:DeliveryNotes><aw:Items><aw:Item aw:PartNumber=\"456-NM\"><aw:ProductName>Power Supply</aw:ProductName><aw:Quantity>1</aw:Quantity><aw:USPrice>45.99</aw:USPrice></aw:Item></aw:Items></aw:PurchaseOrder><aw:PurchaseOrder aw:PurchaseOrderNumber=\"99504\" aw:OrderDate=\"1999-10-22\"><aw:Address aw:Type=\"Shipping\"><aw:Name>Jessica Arnold</aw:Name><aw:Street>4055 Madison Ave</aw:Street><aw:City>Seattle</aw:City><aw:State>WA</aw:State><aw:Zip>98112</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:Address aw:Type=\"Billing\"><aw:Name>Jessica Arnold</aw:Name><aw:Street>4055 Madison Ave</aw:Street><aw:City>Buffalo</aw:City><aw:State>NY</aw:State><aw:Zip>98112</aw:Zip><aw:Country>USA</aw:Country></aw:Address><aw:Items><aw:Item aw:PartNumber=\"898-AZ\"><aw:ProductName>Computer Keyboard</aw:ProductName><aw:Quantity>1</aw:Quantity><aw:USPrice>29.99</aw:USPrice></aw:Item><aw:Item aw:PartNumber=\"898-AM\"><aw:ProductName>Wireless Mouse</aw:ProductName><aw:Quantity>1</aw:Quantity><aw:USPrice>14.99</aw:USPrice></aw:Item></aw:Items></aw:PurchaseOrder></aw:PurchaseOrders>"
}
Code language: JSON / JSON with Comments (json)

Response

[
    {
        "purchaseOrderNumber": 99503,
        "orderDate": "1999-10-20",
        "deliveryNotes": "Please leave packages in shed by driveway."
    },
    {
        "purchaseOrderNumber": 99505,
        "orderDate": "1999-10-22",
        "deliveryNotes": "Please notify me before shipping."
    },
    {
        "purchaseOrderNumber": 99504,
        "orderDate": "1999-10-22",
        "deliveryNotes": null
    }
]
Code language: JSON / JSON with Comments (json)