How to convert XML string to JSON object in Power Automate

So here I am back with another blog on Power Automate and again one simple but widely used functionality. How do you convert XML string in JSON object?

If you are thinking I am going to show you something really hard here, you are in for a surprise. There is an OOB function available in Power Automate which does exactly the same.

So lets’ get started. For demo, I am using a pretty simple XML here.

<Products>
   <Product Name="Product A">
      <Colors>Yellow, Green</Colors>
      <Weight>100-200 gms</Weight>
   </Product>
   <Product Name="Product B">
      <Colors>Yellow, Red</Colors>
      <Weight>150-250 gms</Weight>
   </Product>
</Products>

Although quite basic, the XML has virtually everything. You have an array of products. And then you have properties and attributes as well. For example, Name is an attribute of Product tag and each product has additional properties like Colors and Weight.

Now I need to convert it to JSON object. Let’s see how we can do it.

For this I use a simple Power Automate flow with manual trigger. And then use the Compose action to hold the XML string.

xml to JSON in power automate

In the next step I use the Compose action again to convert the XML string to JSON string.

xml to JSON in power automate
json(xml(outputs('Compose')))

As you can see the expression above, I first convert the XML string into XML object using the xml function. And then use the json function to covert it into JSON string.

When I run the flow, below is the output of the above step.

xml to JSON in power automate

As you can see, Product has been converted to array in JSON and the attribute Name has been identified as well. The next is to parse this JSON array for which I am using the Parse JSON action. Following which I am iterating through each product object.

xml to JSON in power automate

Hope this helped!

Debajit Dutta
Business Solutions MVP