Logic App Best Practices, Tips, and Tricks: #41 How to convert XML into JSON inside Logic Apps

  • Sandro Pereira
  • Apr 3, 2024
  • 4 min read

Welcome back to another Logic Apps Best Practices, Tips, and Tricks. In previous posts, I shared a useful trick for administrators. Today, I shift the focus to developers and explore a simple yet effective best practice: converting XML to JSON in Logic Apps.

We often overcomplicate solutions, follow indirect paths, or rely on obscure shortcuts. I prefer to keep things clean and simple. I focus on approaches that speed up development and remain easy to maintain over time.

convert XML into JSON

📝 One-Minute Brief

Convert XML messages into JSON directly inside Azure Logic Apps by using simple expressions, avoiding external services while keeping workflows clean, efficient, and easy to maintain.

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 we don’t need to make an API Management API call, invoke an Azure Function, or use 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 its 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 it 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()))
  • 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.
json expression
  • 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, we should use the xml() expression alongside the json() expression. This will force the json() expression input to be XML.

The advantages of using json() alongside the xml() expression to convert XML documents into JSON are:

  • They run within the Logic App runtime, which means they execute 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! 

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.

1 thought on “Logic App Best Practices, Tips, and Tricks: #41 How to convert XML into JSON inside Logic Apps”

Leave a Reply

Your email address will not be published. Required fields are marked *

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top