Convert JSON to XML

APPLIES TO: All API Management tiers

The json-to-xml policy converts a request or response body from JSON to XML.

Note

Set the policy's elements and child elements in the order provided in the policy statement. Learn more about how to set or edit API Management policies.

Policy statement

<json-to-xml 
    apply="always | content-type-json" 
    consider-accept-header="true | false" 
    parse-date="true | false" 
    namespace-separator="separator character"
    namespace-prefix="namespace prefix"
    attribute-block-name="name" />

Attributes

Attribute Description Required Default
apply The attribute must be set to one of the following values.

- always - always apply conversion.
- content-type-json - convert only if response Content-Type header indicates presence of JSON.

Policy expressions are allowed.
Yes N/A
consider-accept-header The attribute must be set to one of the following values.

- true - apply conversion if XML is requested in request Accept header.
- false - always apply conversion.

Policy expressions are allowed.
No true
parse-date When set to false date values are simply copied during transformation. Policy expressions aren't allowed. No true
namespace-separator The character to use as a namespace separator. Policy expressions are allowed. No Underscore
namespace-prefix The string that identifies property as namespace attribute, usually "xmlns". Properties with names beginning with specified prefix will be added to current element as namespace declarations. Policy expressions are allowed. No N/A
attribute-block-name When set, properties inside the named object will be added to the element as attributes. Policy expressions are allowed. No Not set

Usage

Example

Consider the following policy:

<policies>
    <inbound>
        <base />
    </inbound>
    <outbound>
        <base />
        <json-to-xml apply="always" consider-accept-header="false" parse-date="false" namespace-separator=":" namespace-prefix="xmlns" attribute-block-name="#attrs" />
    </outbound>
</policies>

If the backend returns the following JSON:

{
  "soapenv:Envelope": {
    "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
    "xmlns:v1": "http://localdomain.com/core/v1",
    "soapenv:Header": {},
    "soapenv:Body": {
      "v1:QueryList": {
        "#attrs": {
          "queryName": "test"
        },
        "v1:QueryItem": {
          "name": "dummy text"
        }
      }
    }
  }
}

The XML response to the client will be:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://localdomain.com/core/v1">
  <soapenv:Header />
  <soapenv:Body>
    <v1:QueryList queryName="test">
      <name>dummy text</name>
    </v1:QueryList>
  </soapenv:Body>
</soapenv:Envelope>

For more information about working with policies, see: