This post was originally published here

Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I discussed a basic trick for developers that was to convert XML into JSON inside Logic Apps. Today we do and speak about the inverse process: How to convert JSON into XML inside Logic Apps.

If you need to convert JSON messages into XML inside your workflows, you don’t need to create an Azure Function, an API, or any other service! You can use out-of-the-box capabilities.

How to convert JSON into XML inside Logic Apps

Once again, today, I want to share something simple.

It is possible to convert a JSON message into XML inside Logic Apps using only expressions. This means that we don’t need to perform an API Management API call, an Azure Function, or any other external service to accomplish this translation, similar to what happens with the opposite process: converting XML into JSON.

Let’s examine a sample to better understand it. The idea is to convert the following JSON into his equivalent XML format:

{
  "person": {
    "name": "Sandro Pereira",
    "city": "Portugal"
  }
}

To try this, we can create a simple Logic App that accepts an HTTP XML request and returns is equivalent in JSON. To do that, we need the following:

  • Create a Logic App and add a When a HTTP request is received trigger.
    • Leave the trigger as is.
  • For better visibility and perception, let’s add an Initialize Variable action – this is optional
    • And set the following expression on the Value property:
      • xml(triggerBody())
  • Finally, add a Response action and set the body to be the variable we initialized before.
    • Note that the previous step is optional because we can use the expression directly in the response.
  • Save your Logic App.

Now, if you try to send a JSON request using, for example, POSTMAN. You will notice that the output will be the message you sent in an XML format:


	Sophia Owen
	Seattle

In case, er have a simple JSON message without an element that can serve as an XML root node like, for example, this one:

{
    "id": "xxxx-xxxx-xxxx",
    "name": "LA-FridayFactTest-POC",
    "type": "Microsoft.Logic/workflows",
    "location": "westeurope",
    "run": {
        "id": "/subscriptions/xxxx-xxxx-xxxx/resourceGroups/xxxx/providers/Microsoft.Logic/workflows/LA-FridayFactTest-POC/runs/xxxxxxxxxxxxxxxxxxxx",
        "name": "xxxxxxxxxxxxxxxxxxxx",
        "type": "Microsoft.Logic/workflows/runs"
    }
}

The runtime will add a default root node called root:


	xxxx-xxxx-xxxx
	LA-FridayFactTest-POC
	Microsoft.Logic/workflows
	westeurope
	
		/subscriptions/xxxx-xxxx-xxxx/resourceGroups/xxxx/providers/Microsoft.Logic/workflows/LA-FridayFactTest-POC/runs/xxxxxxxxxxxxxxxxxxxx
		xxxxxxxxxxxxxxxxxxxx
		Microsoft.Logic/workflows/runs
	

If we want to provide a custom root name for that XML message, we can add a compose action or a variable and add the desired root name by composing a new JSON message like this:

And on the Response action, we will convert the output of the compose action into XML:

Note: don’t try to use the json() expression alongside the xml() expression like this:

  • xml(json(triggerBody()))

Because it is more likely you will get an error message similar to this one:

Unable to process template language expressions in action ‘Compose’ inputs at line ‘0’ and column ‘0’: ‘The template language function ‘json’ expects its parameter to be a string or an XML. The provided value is of type ‘Object’. Please see https://aka.ms/logicexpressions#json for usage details.’.

I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.

Hope you find this helpful! So, if you liked the content or found it useful and want to help me write more, you can help us buy a Star Wars Lego for my son! 

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc.

He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.
View all posts by Sandro Pereira