In this very simple post, let’s look at how you can convert JSON to XML and XML back to JSON while working in Power Automate.
First, let’s look at converting JSON to XML and then, XML to JSON from the same result of the first conversion.
JSON to XML
Let’s look at an example where we have a sample JSON which we will convert to XML in Power Automate using xml() function and we’ll revert the same operation using xml() function in Power Automate itself.
- So starting off with JSON data, you’ll need a String based JSON data. I’ll store the same in a variable which looks like below.
- If I format the same data in JSON formatter online, it’ll look like this –
- Next, we can use the formula xml(<jsonData>) in the expressions and use it as below
Now, since I’m storing my JSON data in String already, I’m converting it to JSON by using json() function inside the xml() function.
- The result of the same is as below
- And if we take it to an XML formatter, it’ll look like below
Let’s look at what won’t work
- Cannot use an Array and there can be only 1 Root element. Hence, the below won’t work –
You cannot have an Array of JSON elements which looks like below –
It will result in the below error saying, “The template language function ‘xml’ parameter is not valid. The provided value cannot be converted to XML:’Data at the root level is invalid…“
- Also, when it is not an Array already, but there are Multiple Attributes at the Root level itself, it won’t work either. Something like below –
Or if we format and look at it, the Name and the Newsletters is the same Root level –
Which will result in the below error ‘The template language function ‘xml’ parameter is not valid. The provided value cannot be converted to XML: ‘JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path ‘Newsletters’.’ –
XML to JSON
Similarly, let’s see how we can inverse the conversion now from XML back to JSON –
- In this post, we are taking the same XML result which we first converted from JSON back to JSON again. But you can start fresh or take the source from elsewhere, of course.
The formula to convert from XML to JSON is
Like in the previous step, the XML was in String as a result captured from the previous step and we need to convert it to XML first in order to convert it to JSON.
- The result is as follows –
- Also, like in the previous JSON to XML conversion, Root level node has to be present. Else, you’ll see the following error in case you don’t have a root for the XML.
And it will result in the below error
Official Microsoft Links for the above functions are –
- JSON – https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#xml?WT.mc_id=DX-MVP-5003911
- XML – https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#json?WT.mc_id=DX-MVP-5003911
Hope this was helpful! Here are some more Power Automate / Flow posts you might want to check –
- Office 365 Outlook connector in Cloud Flows showing Invalid Connection error | Power Automate
- Filter records in a View owned by a Team you are a member of | Dynamics 365 CRM
- FormatDateTime function in a Flow | Power Automate
- Formatting Approvals’ Details in Cloud Flows | Power Automate
- InvalidWorkflowTriggerName or InvalidWorkflowRunActionName error in saving Cloud Flows | Power Automate Quick Tip
- Read OptionSet Labels from CDS/Dataverse Triggers or Action Steps in a Flow | Power Automate
- Using outputs() function and JSON Parse to read data from missing dynamic value in a Flow | Power Automate
- Trigger Conditions not working in a Cloud Flow? Here’s Why | Power Automate Quick Tip
- Make On-Demand Flow to show up in Dynamics 365 | Power Automate
- Run As context in CDS (Current Environment) Flow Trigger | Power Automate
6 thoughts on “Converting JSON to XML and XML to JSON in a Flow | Power Automate”
But I feel its not much useful if we can’t do arrays or element attributes
Yes, I even tried to put an array inside the Root element. Doesn’t work unfortunately. Very limited use of this.
I would like to recommend online tools https://wtools.io/convert-json-to-xml and https://wtools.io/convert-xml-to-json
LikeLiked by 1 person
Thanks for the superb recommendation, Stacy, I’ll do take a look.
you can check: http://www.convert99.com/jsontoxml.html
LikeLiked by 1 person