Convert XML to JSON
APPLIES TO: All API Management tiers
The xml-to-json
policy converts a request or response body from XML to JSON. This policy can be used to modernize APIs based on XML-only backend web services.
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
<xml-to-json kind="javascript-friendly | direct" apply="always | content-type-xml" consider-accept-header="true | false" always-array-child-elements="true | false"/>
Attributes
Attribute | Description | Required | Default |
---|---|---|---|
kind | The attribute must be set to one of the following values. - javascript-friendly - the converted JSON has a form friendly to JavaScript developers.- direct - the converted JSON reflects the original XML document's structure.Policy expressions are allowed. |
Yes | N/A |
apply | The attribute must be set to one of the following values. - always - convert always.- content-type-xml - convert only if response Content-Type header indicates presence of XML.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 JSON is requested in request Accept header.- false -always apply conversion.Policy expressions are allowed. |
No | true |
always-array-child-elements | The attribute must be set to one of the following values. - true - Always convert child elements into a JSON array.- false - Only convert multiple child elements into a JSON array. Convert a single child element into a JSON object.Policy expressions are allowed. |
No | false |
Usage
- Policy sections: inbound, outbound, on-error
- Policy scopes: global, workspace, product, API, operation
- Gateways: classic, v2, consumption, self-hosted, workspace
Example
<policies>
<inbound>
<base />
</inbound>
<outbound>
<base />
<xml-to-json kind="direct" apply="always" consider-accept-header="false" />
</outbound>
</policies>
Related policies
Related content
For more information about working with policies, see:
- Tutorial: Transform and protect your API
- Policy reference for a full list of policy statements and their settings
- Policy expressions
- Set or edit policies
- Reuse policy configurations
- Policy snippets repo
- Author policies using Microsoft Copilot in Azure