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)

Extending on the simple example, the below example includes a sub loop which processes each item contained against each purchase order.

Request

{
    "primaryLoopAtElement": "//aw:PurchaseOrder",
    "map": {
        "purchaseOrderNumber": "@aw:PurchaseOrderNumber",
        "orderDate": "@aw:OrderDate",
        "deliveryNotes": "aw:DeliveryNotes",
        "items": "$poItems"
    },
    "subLoopAtElements": [
        {
            "mapName": "$poItems",
            "xPathQuery": ".//aw:Item",
            "map": {
                "partNumber": "@aw:PartNumber",
                "productName": "aw:ProductName",
                "quantity": "aw:Quantity",
                "usPrice": "aw:USPrice"
            }
        }
    ],    
    "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.",
        "items": [
            {
                "partNumber": "872-AA",
                "productName": "Lawnmower",
                "quantity": 1,
                "usPrice": 148.95
            },
            {
                "partNumber": "926-AA",
                "productName": "Baby Monitor",
                "quantity": 2,
                "usPrice": 39.98
            }
        ]
    },
    {
        "purchaseOrderNumber": 99505,
        "orderDate": "1999-10-22",
        "deliveryNotes": "Please notify me before shipping.",
        "items": [
            {
                "partNumber": "456-NM",
                "productName": "Power Supply",
                "quantity": 1,
                "usPrice": 45.99
            }
        ]
    },
    {
        "purchaseOrderNumber": 99504,
        "orderDate": "1999-10-22",
        "deliveryNotes": null,
        "items": [
            {
                "partNumber": "898-AZ",
                "productName": "Computer Keyboard",
                "quantity": 1,
                "usPrice": 29.99
            },
            {
                "partNumber": "898-AM",
                "productName": "Wireless Mouse",
                "quantity": 1,
                "usPrice": 14.99
            }
        ]
    }
]
Code language: JSON / JSON with Comments (json)

Extending on the previous sub loop example, the below example shows additional selections that can be made on attributes as well as the additional more advanced settings that will affect the behaviour of the outcome.

The entire suite of 100+ custom functions baked into the framework are able to be utilised through this operation. As stated in the documentation, all that is required is to prefix the function with the relevant namespace, ado:

The below example demonstrates the more advanced capability of this operation.

Request

{
    "primaryLoopAtElement": "//aw:PurchaseOrder",
    "map": {
        "purchaseOrderNumber": "@aw:PurchaseOrderNumber",
        "orderDate": "ado:FormatDateTime(ado:DateTimeParseExact(.//@aw:OrderDate, 'yyyy-MM-dd'), 'dd MMM yyyy')",
        "deliveryNotes": "aw:DeliveryNotes",
        "address": {
            "shipping": {
                "name": "aw:Address[@aw:Type='Shipping']/aw:Name",
                "address": "concat(concat(concat(concat(concat(concat(concat(concat(//aw:Address[@aw:Type='Shipping']/aw:Street, ', '), //aw:Address[@aw:Type='Shipping']/aw:City), ', '), //aw:Address[@aw:Type='Shipping']/aw:State), ', '), //aw:Address[@aw:Type='Shipping']/aw:Zip), ', '), //aw:Address[@aw:Type='Shipping']/aw:Country)"
            },
            "billing": {
                "name": "aw:Address[@aw:Type='Billing']/aw:Name",
                "address": "concat(concat(concat(concat(concat(concat(concat(concat(//aw:Address[@aw:Type='Billing']/aw:Street, ', '), //aw:Address[@aw:Type='Billing']/aw:City), ', '), //aw:Address[@aw:Type='Billing']/aw:State), ', '), //aw:Address[@aw:Type='Billing']/aw:Zip), ', '), //aw:Address[@aw:Type='Billing']/aw:Country)"
            }
        },
        "items": "$poItems"
    },
    "subLoopAtElements": [
        {
            "mapName": "$poItems",
            "xPathQuery": ".//aw:Item",
            "map": {
                "partNumber": "@aw:PartNumber",
                "productName": "aw:ProductName",
                "quantity": "aw:Quantity",
                "usPrice": "concat('$', //aw:USPrice)"
            }
        }
    ],
    "settings": {
        "convertAnythingThatLooksLikeANumberToANumber": true,
        "setEmptyValuesToNull": true,
        "errorOnMultipleNodeQueryResult": false,
        "multipleNodeQueryResultSelectionType": "All"
    },
    "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": "20 Oct 1999",
        "deliveryNotes": "Please leave packages in shed by driveway.",
        "address": {
            "shipping": {
                "name": "Ellen Adams",
                "address": "123 Maple Street, Mill Valley, CA, 10999, USA"
            },
            "billing": {
                "name": "Tai Yee",
                "address": "8 Oak Avenue, Old Town, PA, 95819, USA"
            }
        },
        "items": [
            {
                "partNumber": "872-AA",
                "productName": "Lawnmower",
                "quantity": 1,
                "usPrice": "$148.95"
            },
            {
                "partNumber": "926-AA",
                "productName": "Baby Monitor",
                "quantity": 2,
                "usPrice": "$39.98"
            }
        ]
    },
    {
        "purchaseOrderNumber": 99505,
        "orderDate": "22 Oct 1999",
        "deliveryNotes": "Please notify me before shipping.",
        "address": {
            "shipping": {
                "name": "Cristian Osorio",
                "address": "456 Main Street, Buffalo, NY, 98112, USA"
            },
            "billing": {
                "name": "Cristian Osorio",
                "address": "456 Main Street, Buffalo, NY, 98112, USA"
            }
        },
        "items": [
            {
                "partNumber": "456-NM",
                "productName": "Power Supply",
                "quantity": 1,
                "usPrice": "$45.99"
            }
        ]
    },
    {
        "purchaseOrderNumber": 99504,
        "orderDate": "22 Oct 1999",
        "deliveryNotes": null,
        "address": {
            "shipping": {
                "name": "Jessica Arnold",
                "address": "4055 Madison Ave, Seattle, WA, 98112, USA"
            },
            "billing": {
                "name": "Jessica Arnold",
                "address": "4055 Madison Ave, Buffalo, NY, 98112, USA"
            }
        },
        "items": [
            {
                "partNumber": "898-AZ",
                "productName": "Computer Keyboard",
                "quantity": 1,
                "usPrice": "$29.99"
            },
            {
                "partNumber": "898-AM",
                "productName": "Wireless Mouse",
                "quantity": 1,
                "usPrice": "$14.99"
            }
        ]
    }
]
Code language: JSON / JSON with Comments (json)

Extending on the simple example, the below example includes a sub loop which processes each item contained against each purchase order.

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

In the PowerAutomate connector, this approach is unlikely to work. The connector expects an array of objects to be returned but a straight Xml to Json conversion will return an object.

To use this operation in PowerAutomate, you will need to bypass the our connector and use the built-in HTTP operation.

If a straight conversion to JSON is required, this is possible by simply omitting all properties apart from data.

The framework will then simply transform the entire XML payload, including all elements and attributes, and return the resulting JSON structure.

Note: All optional settings will not be factored in when using this conversion mechanism.

Request

{
    "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

{
    "?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:Street": "123 Maple Street",
                        "aw:City": "Mill Valley",
                        "aw:State": "CA",
                        "aw:Zip": "10999",
                        "aw:Country": "USA"
                    },
                    {
                        "@aw:Type": "Billing",
                        "aw:Name": "Tai Yee",
                        "aw:Street": "8 Oak Avenue",
                        "aw:City": "Old Town",
                        "aw:State": "PA",
                        "aw:Zip": "95819",
                        "aw:Country": "USA"
                    }
                ],
                "aw:DeliveryNotes": "Please leave packages in shed by driveway.",
                "aw:Items": {
                    "aw:Item": [
                        {
                            "@aw:PartNumber": "872-AA",
                            "aw:ProductName": "Lawnmower",
                            "aw:Quantity": "1",
                            "aw:USPrice": "148.95",
                            "aw:Comment": "Confirm this is electric"
                        },
                        {
                            "@aw:PartNumber": "926-AA",
                            "aw:ProductName": "Baby Monitor",
                            "aw:Quantity": "2",
                            "aw:USPrice": "39.98",
                            "aw:ShipDate": "1999-05-21"
                        }
                    ]
                }
            },
            {
                "@aw:PurchaseOrderNumber": "99505",
                "@aw:OrderDate": "1999-10-22",
                "aw:Address": [
                    {
                        "@aw:Type": "Shipping",
                        "aw:Name": "Cristian Osorio",
                        "aw:Street": "456 Main Street",
                        "aw:City": "Buffalo",
                        "aw:State": "NY",
                        "aw:Zip": "98112",
                        "aw:Country": "USA"
                    },
                    {
                        "@aw:Type": "Billing",
                        "aw:Name": "Cristian Osorio",
                        "aw:Street": "456 Main Street",
                        "aw:City": "Buffalo",
                        "aw:State": "NY",
                        "aw:Zip": "98112",
                        "aw:Country": "USA"
                    }
                ],
                "aw:DeliveryNotes": "Please notify me before shipping.",
                "aw:Items": {
                    "aw:Item": {
                        "@aw:PartNumber": "456-NM",
                        "aw:ProductName": "Power Supply",
                        "aw:Quantity": "1",
                        "aw:USPrice": "45.99"
                    }
                }
            },
            {
                "@aw:PurchaseOrderNumber": "99504",
                "@aw:OrderDate": "1999-10-22",
                "aw:Address": [
                    {
                        "@aw:Type": "Shipping",
                        "aw:Name": "Jessica Arnold",
                        "aw:Street": "4055 Madison Ave",
                        "aw:City": "Seattle",
                        "aw:State": "WA",
                        "aw:Zip": "98112",
                        "aw:Country": "USA"
                    },
                    {
                        "@aw:Type": "Billing",
                        "aw:Name": "Jessica Arnold",
                        "aw:Street": "4055 Madison Ave",
                        "aw:City": "Buffalo",
                        "aw:State": "NY",
                        "aw:Zip": "98112",
                        "aw:Country": "USA"
                    }
                ],
                "aw:Items": {
                    "aw:Item": [
                        {
                            "@aw:PartNumber": "898-AZ",
                            "aw:ProductName": "Computer Keyboard",
                            "aw:Quantity": "1",
                            "aw:USPrice": "29.99"
                        },
                        {
                            "@aw:PartNumber": "898-AM",
                            "aw:ProductName": "Wireless Mouse",
                            "aw:Quantity": "1",
                            "aw:USPrice": "14.99"
                        }
                    ]
                }
            }
        ]
    }
}
Code language: JSON / JSON with Comments (json)

This example shows a nested XML structure that is ultimately flattened by the conversion process.

Request

{
    "primaryLoopAtElement": "//aw:Item",
    "map": {
        "PurchaseOrderNo": "ancestor::aw:PurchaseOrder/@aw:PurchaseOrderNumber",
        "OrderDate": "ancestor::aw:PurchaseOrder/@aw:OrderDate",
        "ShippingName": "../../aw:Address[@aw:Type=\"Shipping\"]/aw:Name",
        "ShippingStreet": "../../aw:Address[@aw:Type=\"Shipping\"]/aw:Street",
        "ShippingCity": "../../aw:Address[@aw:Type=\"Shipping\"]/aw:City",
        "ShippingState": "../../aw:Address[@aw:Type=\"Shipping\"]/aw:State",
        "BillingName": "../../aw:Address[@aw:Type=\"Billing\"]/aw:Name",
        "BillinggStreet": "../../aw:Address[@aw:Type=\"Billing\"]/aw:Street",
        "BillingCity": "../../aw:Address[@aw:Type=\"Billing\"]/aw:City",
        "BillingState": "../../aw:Address[@aw:Type=\"Billing\"]/aw:State",
        "PartNumber": "@aw:PartNumber",
        "ProductName": "aw:ProductName",
        "Quantity": "aw:Quantity",
        "USPrice": "aw:USPrice",
        "ShipDate": "aw:ShipDate",
        "Comment": "aw:Comment"
    },
    "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

[
    {
        "PurchaseOrderNo": 99503,
        "OrderDate": "1999-10-20",
        "ShippingName": "Ellen Adams",
        "ShippingStreet": "123 Maple Street",
        "ShippingCity": "Mill Valley",
        "ShippingState": "CA",
        "BillingName": "Tai Yee",
        "BillinggStreet": "8 Oak Avenue",
        "BillingCity": "Old Town",
        "BillingState": "PA",
        "PartNumber": "872-AA",
        "ProductName": "Lawnmower",
        "Quantity": 1,
        "USPrice": 148.95,
        "ShipDate": null,
        "Comment": "Confirm this is electric"
    },
    {
        "PurchaseOrderNo": 99503,
        "OrderDate": "1999-10-20",
        "ShippingName": "Ellen Adams",
        "ShippingStreet": "123 Maple Street",
        "ShippingCity": "Mill Valley",
        "ShippingState": "CA",
        "BillingName": "Tai Yee",
        "BillinggStreet": "8 Oak Avenue",
        "BillingCity": "Old Town",
        "BillingState": "PA",
        "PartNumber": "926-AA",
        "ProductName": "Baby Monitor",
        "Quantity": 2,
        "USPrice": 39.98,
        "ShipDate": "1999-05-21",
        "Comment": null
    },
    {
        "PurchaseOrderNo": 99505,
        "OrderDate": "1999-10-22",
        "ShippingName": "Cristian Osorio",
        "ShippingStreet": "456 Main Street",
        "ShippingCity": "Buffalo",
        "ShippingState": "NY",
        "BillingName": "Cristian Osorio",
        "BillinggStreet": "456 Main Street",
        "BillingCity": "Buffalo",
        "BillingState": "NY",
        "PartNumber": "456-NM",
        "ProductName": "Power Supply",
        "Quantity": 1,
        "USPrice": 45.99,
        "ShipDate": null,
        "Comment": null
    },
    {
        "PurchaseOrderNo": 99504,
        "OrderDate": "1999-10-22",
        "ShippingName": "Jessica Arnold",
        "ShippingStreet": "4055 Madison Ave",
        "ShippingCity": "Seattle",
        "ShippingState": "WA",
        "BillingName": "Jessica Arnold",
        "BillinggStreet": "4055 Madison Ave",
        "BillingCity": "Buffalo",
        "BillingState": "NY",
        "PartNumber": "898-AZ",
        "ProductName": "Computer Keyboard",
        "Quantity": 1,
        "USPrice": 29.99,
        "ShipDate": null,
        "Comment": null
    },
    {
        "PurchaseOrderNo": 99504,
        "OrderDate": "1999-10-22",
        "ShippingName": "Jessica Arnold",
        "ShippingStreet": "4055 Madison Ave",
        "ShippingCity": "Seattle",
        "ShippingState": "WA",
        "BillingName": "Jessica Arnold",
        "BillinggStreet": "4055 Madison Ave",
        "BillingCity": "Buffalo",
        "BillingState": "NY",
        "PartNumber": "898-AM",
        "ProductName": "Wireless Mouse",
        "Quantity": 1,
        "USPrice": 14.99,
        "ShipDate": null,
        "Comment": null
    }
]
Code language: JSON / JSON with Comments (json)