This post was originally published here

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:


  Sandro Pereira
  961098121
  A1245FD5
  5
  12345
  SN9019
  2015-09-11
  0
  0
  0
  10
  
    
      1
      1
      10
      10
    
    
      1
      2
      5
      11
    
  
  PreOrder
  
  0
  
    Crestuma
    Portugal
    Crestuma
    sandro.pereira@devscope.net
    sandro-pereira@live.com.pt
    
    Porto
    4415
    961098121
  

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()))
  • 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! 

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