'How to prevent alphabetical sorting when converting from JSON to XML in Azure Logic App

I'm using the xml() function in Azure Logic app to convert a JSON formatted text to XML.

My function:

xml(json(concat('{"root":', string(variables('JsonObject')), '}')))

My issue is that the XML file on conversation automatically sorts itself in alphabetical order. I need the order of elements in XML to be the same as the order in my JSON text.

Please noticed how the order of elements has been sorted in below examples.

JSON INPUT EXAMPLE:

{
    "Employee": [
        {
            "Company": [
                {
                    "Code": "",
                    "Name": "",
                    "ActiveUntil": "2021-12-12T00:00:00",
                    "CompanyType": [
                        {
                            "Code": ""
                        }
                    ]
                }
            ]
        },
        {
            "EmailAddress": "",
            "GivenName": "",
            "MobilePhoneNumber": "",
            "EndDate": "2021-12-12T00:00:00",
            "Surname": "",
            "Updated": "2021-12-12T00:00:00"
        }
    ]
}

XML OUTPUT:

<Employees>
<Employee>
    <Company>
        <ActiveUntil>2021-12-12T00:00:00</ActiveUntil>
        <Code></Code>
        <CompanyType>
            <Code></Code>
        </CompanyType>
        <Name></Name>
    </Company>
</Employee>
<Employee>
    <EmailAddress></EmailAddress>
    <EndDate>2021-12-12T00:00:00</EndDate>
    <GivenName></GivenName>
    <MobilePhoneNumber></MobilePhoneNumber>
    <Surname></Surname>
    <Updated>2021-12-12T00:00:00</Updated>
</Employee>


Solution 1:[1]

You are using a JSON object (elsewhere called a dictionary or map). The order of elements is not defined. A JSON object with the same key/value pairs in different order is the same. The XML conversion can’t change the order because there is no order in the first place.

If you want an order, use an array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gnasher729