Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I discussed one good trick for administrators. Today we focus on a best practice trip and trick for developers: How to convert XML into JSON inside Logic Apps.
Sometimes, we tend to overcomplicate things, do not follow direct paths, or use obscure shortcuts. I like to keep things clean and simple and use strategies that can improve and speed up our work. I also think about the best way to manage those processes in the long term without overcomplicating things.
How to convert XML into JSON inside Logic Apps
Today I want to share something simple.
It is possible to convert an XML message into JSON inside Logic Apps using 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.
Let’s examine a sample to better understand it. The idea is to convert the following XML into his equivalent JSON format:
<ns0:CustomerOrder xmlns:ns0="http://Northwind.BusinessSolution.Schemas.CustomerOrder">
<CustomerName>Sandro Pereira</CustomerName>
<CustomerContact>961098121</CustomerContact>
<CustomerPONumber>A1245FD5</CustomerPONumber>
<Priority>5</Priority>
<QuoteReferenceId>12345</QuoteReferenceId>
<SalesOrderReferenceID>SN9019</SalesOrderReferenceID>
<CreatedDate>2015-09-11</CreatedDate>
<FreightAmount>0</FreightAmount>
<MiscCharges>0</MiscCharges>
<SubTotal>0</SubTotal>
<TotalOrderAmount>10</TotalOrderAmount>
<Lines>
<SaleOrderLine>
<LineNumber>1</LineNumber>
<UnitPrice>1</UnitPrice>
<Quantity>10</Quantity>
<ItemID>10</ItemID>
</SaleOrderLine>
<SaleOrderLine>
<LineNumber>1</LineNumber>
<UnitPrice>2</UnitPrice>
<Quantity>5</Quantity>
<ItemID>11</ItemID>
</SaleOrderLine>
</Lines>
<Status>PreOrder</Status>
<Comments></Comments>
<UnitPrice>0</UnitPrice>
<ns1:ShippingAddress xmlns:ns1="http://Northwind.BusinessSolution.Schemas.SchemaCommonStructures">
<City>Crestuma</City>
<Country>Portugal</Country>
<Street>Crestuma</Street>
<Email>sandro.pereira@devscope.net</Email>
<Email>sandro-pereira@live.com.pt</Email>
<Fax></Fax>
<State>Porto</State>
<Zip>4415</Zip>
<Phone>961098121</Phone>
</ns1:ShippingAddress>
</ns0:CustomerOrder>
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:
- json(xml(triggerBody()))
- And set the following expression on the Value property:
- 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 an XML request using, for example, POSTMAN. You will notice that the output will be the message you sent in a JSON format:
{
"ns0:CustomerOrder": {
"@xmlns:ns0": "http://Northwind.BusinessSolution.Schemas.CustomerOrder",
"CustomerName": "Sandro Pereira",
"CustomerContact": "961098121",
"CustomerPONumber": "A1245FD5",
"Priority": "5",
"QuoteReferenceId": "12345",
"SalesOrderReferenceID": "SN9019",
"CreatedDate": "2015-09-11",
"FreightAmount": "0",
"MiscCharges": "0",
"SubTotal": "0",
"TotalOrderAmount": "10",
"Lines": {
"SaleOrderLine": [
{
"LineNumber": "1",
"UnitPrice": "1",
"Quantity": "10",
"ItemID": "10"
},
{
"LineNumber": "1",
"UnitPrice": "2",
"Quantity": "5",
"ItemID": "11"
}
]
},
"Status": "PreOrder",
"Comments": "",
"UnitPrice": "0",
"ns1:ShippingAddress": {
"@xmlns:ns1": "http://Northwind.BusinessSolution.Schemas.SchemaCommonStructures",
"City": "Crestuma",
"Country": "Portugal",
"Street": "Crestuma",
"Email": [
"sandro.pereira@devscope.net",
"sandro-pereira@live.com.pt"
],
"Fax": "",
"State": "Porto",
"Zip": "4415",
"Phone": "961098121"
}
}
}
Note that when we want to convert an XML message into JSON, you should use the xml() expression alongside the json() expression. This will force the json() expression input to be an XML.
The advantages of using json() alongside the xml() expression to convert XML documents into JSON are:
- They run inside the Logic App runtime, which means they are executed faster;
- Expressions are not paid like actions, meaning you can use fewer actions inside your Logic App. Instead, you can use them to define the action inputs. This will minimize workflow complexity and also save on costs (a little bit).
Sometimes, we wish that all things could be this simple and straightforward, but for the ones that are, let’s enjoy them!
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.
Thanks to Luis Rigueira for this idea for 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!
Hi Sandro! Thanks for great blog posts!
A small tip from me regarding XML transformation is to use https://www.weavo.dev/ to generate liquid templates that can be used in the Logic Apps.