Was this helpful?

The API Platform enables developers to convert messages from the extensible markup language (XML) format to JavaScript object notation (JSON) format to the by using the XMLtoJSON policy type. This policy is useful for enabling backend XML services to support RESTful apps that require JSON (for example, due to a lack of an XML parsing capabilities on the client).

In a typical mediation scenario, a JSONtoXML policy on the inbound request flow is often paired with an XMLtoJSON policy on the outbound response flow. By combining policies this way, a JSON API can be exposed for backend services that natively support only XML.

For scenarios where APIs are consumed by diverse clients apps which may require either JSON and XML, the format of the response can be dynamically set by configuring JSONtoXML and XMLtoJSON policies to execute conditionally. See the API Platform Developer Guide topic Customize responses for mobile devices for an implementation of this scenario.

The HTTP Content-type header of the source message must be set to text/xml. If the HTTP Content-type header of the source message is not text/xml, the policy is not enforced.

The payload of the XML message is parsed and converted into JSON, and the HTTP Content-type header of the XML-formatted message is set to application/json.

Sample

This policy type defines a set of reasonable defaults. The minimal policy configuration required to convert XML to JSON is the following:

<XMLToJSON name="ConvertToJSON">
  <Options>
  </Options>
  <OutputVariable>response</OutputVariable>
  <Source>response</Source>
</XMLToJSON>

Assuming that the intent is to convert an XML-formatted response into a JSON-formatted response, the policy would be attached to a response Flow (for example, the ProxyEndpoint response PostFlow). This configuration takes a JSON-formatted response message as the source, and then creates an XML-formatted message that is populated in the response OutputVariable. The API Platform automatically uses the content of this variable as the message for next processing step.

Configuring an XML to JSON policy

For more advanced control over mapping XML to JSON structures, use the following configuration elements. All fields are optional. However, if you specify NamespaceBlockName, you must also specify DefaultNamespaceNodeName and NamespaceSeparator.

Field Name Description
Source
The variable that contains the XML message to convert to JSON.
  • If source is not defined, then it is treated as a simple message (request or response). Usually, you set this to be request or response, depending on whether you need to convert an inbound XML request, or an outbound XML response, into JSON.
  • If the source variable cannot be resolved, or resolves to a non-message type, the policy execution fails.
OutputVariable
Stores the output of the XML to JSON format conversion. This is usually the same value as the source, that is, usually inbound XML request in converted to an inbound JSON request,
If OutputVariable is not specified, the source is treated as OutputVariable. For example, if the source is response, then OutputVariable defaults to response.
RecognizeNumber
Valid values: true false
Default is false.
If true, then number fields in the XML payload retain their original format.
Consider the following XML example:
<a>
  <b>100</b>
  <c>value</c>
</a>
If true, converts to:
{
    "a": {
        "b": 100,
        "c": "value"
    }
}
If false, converts to:
{
    "a": {
        "b": "100",
        "c": "value"
    }
}
RecognizeBoolean
Valid values: true and false
The default value is

false

For the following XML example:
<a>
  <b>true</b>
  <c>value</c>
</a>
If true, converts to:
{
    "a": {
        "b": true,
        "c": "value"
    }
}
If false, converts to:
{
    "a": {
        "b": "true",
        "c": "value"
    }
}
RecognizeNull
Valid values: true and

false

Default value is

false

.
For the following XML:
<a>
  <b></b>
  <c>value</c>
</a>
If true, converts to:
{
  "a": {
    "b": null,
    "c": "value"
  }
}
If false, converts to:
{
  "a": {
    "b": {},
    "c": "value"
  }
}
NullValue Indicates a null value. By default the value is NULL.
NamespaceSeparator
Consider the following XML example:
<a xmlns="http://ns.com" xmlns:ns1="http://ns1.com">
  <ns1:b>value</ns1:b>
</a>
If NamespaceSeparator is not specified, the following JSON structure is generated:
{
    "a": {
        "b": "value"
    }
}
If the attributes NamespaceSeparator, NamespaceBlockName, and DefaultNamespaceNodeName are all specified, the following JSON structure is generated:
{
    "a": {
        "#namespaces": {
            "&": "http://ns.com",
            "ns1": "http://ns1.com"
        },
        "ns1%b": "value"
    }
}
NamespaceBlockName
DefaultNamespaceNodeName
TextAlwaysAsProperty
Valid values: true/false
Default is false.
If set to true, the content of the XML element is converted to a string property.
For the following XML:
<a>
  <b>value1</b>
  <c>value2<d>value3</d>value4</c>
</a>
If TextAlwaysAsProperty is set to true and TextNodeName specified as TEXT, the following JSON structure is generated:
{
    "a": {
        "b": {
            "TEXT": "value1"
        },
        "c": {
            "TEXT": [
                "value2",
                "value4"
            ],
            "d": {
                "TEXT": "value3"
            }
        }
    }
}
If TextAlwaysAsProperty is set to false and TextNodeName specified as TEXT, the following JSON structure is generated:
{
    "a": {
        "b": "value1",
        "c": {
            "TEXT": [
                "value2",
                "value4"
            ],
            {
                "d": "value3",
                
            }
        }
    }
TextNodeName
AttributeBlockName
Consider the following XML example:
<a att1="value1" att2="value2"/>
If both the attributes (AttributeBlockName and AttributePrefix) are specified as defined in the XML to JSON example, the following JSON structure is generated:
{
    "a": {
        "ATT_BLOCK": {
            "@att1": "value1",
            "@att2": "value2"
        }
    }
}
If only AttributeBlockName is specified, the following JSON structure is generated:
{
    "a": {
        "ATT_BLOCK": {
            "att1": "value1",
            "att2": "value2"
        }
    }
}
If only AttributePrefix is specified, the following JSON structure is generated:
{
    "a": {
        "@att1": "value1",
        "@att2": "value2"
    }
}
If neither is specified, the following JSON structure is generated:
{
    "a": {
        "att1": "value1",
        "att2": "value2"
    }
}
AttributePrefix
OutputPrefix
Consider the following XML example:
<a>value</a>
If both the attributes (OutputPrefix and OutputSuffix) are specified as defined in the XML to JSON example, the following JSON structure is generated:
PREFIX_{
    "a": "value"
}_SUFFIX
If only OutputPrefix is specified, the following JSON structure is generated:
PREFIX_{
  "a" : "value"
}
If only OutputSuffix is specified, the following JSON structure is generated:
{
  "a" : "value"
}_SUFFIX
If neither OutputPrefix nor OutputSuffix is specified, the following JSON structure is generated:
{
    "a": "value"
}
OutputSuffix

Policy schema

Each policy must conform to a policy schema. All policy constructs such as elements and attributes mentioned above are defined in a schema. To download the schema, click here.

Example - XML to JSON

<XMLToJSON name="mypolicy">
    <Source>request</Source>
    <OutputVariable>response</OutputVariable>
    <Options>
        <RecognizeNumber>true</RecognizeNumber>
        <RecognizeBoolean>true</RecognizeBoolean>
        <RecognizeNull>true</RecognizeNull>
        <NullValue>NULL</NullValue>
        <NamespaceSeparator>%</NamespaceSeparator>
        <NamespaceBlockName>#namespaces</NamespaceBlockName>
        <DefaultNamespaceNodeName>&</DefaultNamespaceNodeName>
        <TextAlwaysAsProperty>false</TextAlwaysAsProperty>
        <TextNodeName>TEXT</TextNodeName>
        <AttributeBlockName>ATT_BLOCK</AttributeBlockName>
        <AttributePrefix>@</AttributePrefix>
        <OutputPrefix>PREFIX_</OutputPrefix>
        <OutputSuffix>_SUFFIX</OutputSuffix>
    </Options>
</XMLToJSON>

<!--Note that you can use format definition to configure the options. The format definition contains all the <options> elements mentioned in the above example (see the below example).-->

<XMLToJSON name="mypolicy">
    <Source>request</Source>
    <OutputVariable>response</OutputVariable>
    <Format>myFormat</Format>
</XMLToJSON>