'json-to-xml' Azure API Policy Statement


How to use "json-to-xml" Azure API Policy Statement?

✍: FYIcenter.com


The "json-to-xml" Policy Statement allows you to convert the body of the inbound request or outbound response from JSON format to XML format.

For example, the following "json-to-xml" policy statement converts the output response from JSON format to XML format.

    <base />  
    <json-to-xml apply="always" consider-accept-header="false" />  

Two attributes used in the above "xml-to-json" policy statement indicate:

  • apply="always" - Always apply the conversion, regardless of what value is given in the Content-Type header.
  • consider-accept-header="false" - Do not respect the Accept header value and always apply the conversion.

For example, Azure receives the following JSON document in the response body from the backend service:

  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  "phoneNumbers": [
      "type": "home",
      "number": "212 555-1234"
      "type": "mobile",
      "number": "123 456-7890"
  "children": [],
  "spouse": null

The "json-to-xml" policy statement will convert it to an XML document:

        <streetAddress>21 2nd Street</streetAddress>
        <city>New York</city>
        <number>212 555-1234</number>
        <number>123 456-7890</number>
    <children />
    <spouse xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

Note that:

  • The root element is added to the XML document named as "Document".
  • JSON array is converted with a sequence of repeating XML elements.
  • JSON Null value is converted to an XML attribute xsi:nil="true".


'xsl-transform' Azure API Policy Statement

'xml-to-json' on Invalid XML

Policy to Modify Request and Response Body

⇑⇑ Microsoft Azure API Management Tutorials

2023-04-25, 6146🔥, 2💬