'xml-to-json' Policy Statement

Q

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

✍: FYIcenter.com

A

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

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

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

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

  • kind="direct" - The converted JSON reflects the original XML document's structure.
  • 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 XML document in the response body from the backend service:

<?xml version="1.0"?>
<soap:Envelope xmlns="http://fyicenter.com/" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetOrders>
            <Customer>1234567</Customer>
        </GetOrders>
    </soap:Body>
</soap:Envelope>

The "xml-to-json" policy statement will convert it to a JSON string:

{
    "version": "1.0",
    "soap$Envelope": {
        "@xmlns": "http://fyicenter.com/",
        "@xmlns$soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "soap$Body": {
            "GetOrders": {
                "Customer": "1234567"
            }
        }
    }
}

Note that:

  • The XML process instruction "<?xml version="1.0"?> is converted to a JSON object property at the top level. This does not make sense!.
  • Namespace prefix is element names are maintained. But the delimiter ":" is converted to "$". For example, "soap:Envelope" becomes "soap$Envelope".
  • XML attributes are converted to JSOB object properties with their name prefixed with "@". For example, xmlns="http://fyicenter.com/" becomes "@xmlns": "http://fyicenter.com/".

 

Policy to Modify Request and Response Body

⇒⇒Microsoft Azure API Management Tutorials

2017-09-23, 116👍, 0💬