I don’t know the reason why two products from the same family – Azure Integration Services – have completely different behaviors while converting XML to JSON, but that is the current reality. It is a fact! API Management and Logic Apps have different behaviors while applying this conversion, and that is one of the main reasons that I decided to create an Azure Function to convert XML into JSON, to keep the consistency between these two products.
While using API Management, we can use the xml-to-json policy to convert a request or response body from XML to JSON. However, when dealing with XML namespaces and prefixes, which is quite normal when working with XML messages, the policy has, in my opinion, a strange conversion behavior:
- It converts the prefixes that in XML are represented by prefix:MyField into prefix$MyField. In order words, it replaces the colon character (:) with the dollar character ($).
Let’s take this XML sample in order for you to see the upcome result of that xml-to-json policy:
<section xmlns="http://www.test.com/events" xmlns:bk="urn:loc.gov:books" xmlns:pi="urn:personalInformation" xmlns:isbn='urn:ISBN:0-999-99999-9'>
<title>Book-Signing Event</title>
<signing>
<bk:author pi:title="Mr" pi:name="My Name"/>
<book bk:title="How cool is XML" isbn:number="9999999999"/>
<comment xmlns=''>Convert it to JSON</comment>
</signing>
</section>
The result will be:
{
"section": {
"@xmlns": "http://www.test.com/events",
"@xmlns$bk": "urn:loc.gov:books",
"@xmlns$pi": "urn:personalInformation",
"@xmlns$isbn": "urn:ISBN:0-999-99999-9",
"title": "Book-Signing Event",
"signing": {
"bk$author": {
"@pi$title": "Mr",
"@pi$name": "My Name"
},
"book": {
"@bk$title": "How cool is XML",
"@isbn$number": "9999999999"
},
"comment": {
"@xmlns": "",
"#text": "Convert it to JSON"
}
}
}
}
I think this behavior is strange and incorrect.
Now, if we take the same XML payload and try to convert it inside Logic Apps using the json() expression that returns the JSON type value or object for a string or XML. In this case, using, for example, the following expression:
- json(xml(triggerBody()))
The result will be:
{
"section": {
"@xmlns": "http://www.test.com/events",
"@xmlns:bk": "urn:loc.gov:books",
"@xmlns:pi": "urn:personalInformation",
"@xmlns:isbn": "urn:ISBN:0-999-99999-9",
"title": "Book-Signing Event",
"signing": {
"bk:author": {
"@pi:title": "Mr",
"@pi:name": "My Name"
},
"book": {
"@bk:title": "How cool is XML",
"@isbn:number": "9999999999"
},
"comment": {
"@xmlns": "",
"#text": "Convert it to JSON"
}
}
}
}
In this case, the json() expression does not replace the colon character (:) with the dollar character ($) in the prefixes. It’s maintaining, which I think is the correct behavior.
To lazy to read? We’ve got you covered! Check out our video version of this content!
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 Sandro’s son!
I’m facing an issue in this APIM policy (xml-to-json) I don’t know why but when I’m using consumption logicapp I got the body transformed in json like your sample but when I’m using standard logicApp I’m getting no body inside the logocApp what is very strange because I’m using the same policy. Did I forget something about standard logicApp?
I get the same result behavior in Logic App Standard and Consumption. Both work fine, I use the following expression json(xml(triggerBody()))
Hi Snadro Pereira, I am following your articles for quite sometime. I have a challenge that I am trying to get some help. I have multiple claims in one ST/SE segment in my 837P file. do you know how to disassemble the file into single claims using Azure EDI disassemble. I made the required changes in 837P schema. (https://learn.microsoft.com/en-us/biztalk/core/splitting-hipaa-subdocuments) Appreciate your help. Thank you Hanu