XML to JSON

Use the XML to JSON policy to convert the context payload of your API from the extensible markup language (XML) format to JavaScript Object Notation (JSON).

Gateway support

Table 1. Table showing which gateways support this policy, and the corresponding policy version
Gateway Policy version
DataPower® Gateway (v5 compatible) 1.0.0
DataPower API Gateway 2.0.0

This topic describes how to configure the policy in the assembly user interface; for details on how to configure the policy in your OpenAPI source, see xml-to-json.

About

The XML to JSON policy uses a simple convention, based on BadgerFish, to convert your API context payload from XML to JSON. The XML content is preserved, including the attributes and namespaces. No additional configuration is required. For more information about the BadgerFish convention, including some examples, see BadgerFish.

You can attach this policy to the following API flows:
  • REST
  • SOAP

Use the API Manager assembly view when you are creating your API definition to add a built-in policy to the flow.

The policy must be attached to the flow at the point at which you require the conversion to be performed. For example, if you need to convert an XML-formatted request into a JSON-formatted request, the policy must be attached to the request flow.

The policy reads input from the message.body, if that context exists, otherwise from the request.body, and then writes the output to the message.body.

Note: If you are using the DataPower API Gateway, the input to the XML to JSON policy must be parsed data. One way to produce parsed data is to use a Parse policy before an XML to JSON policy in your assembly flow, which provides explicit control of the parse action.

Properties

The following table lists the policy properties, indicates whether a property is required, specifies the valid and default values for input, and specifies the data type of the values.

Table 2. Policy properties
Property label Required Description Data type
Title Yes The title of the policy.

The default value is xml-to-json.

string
Description No A description of the policy. string
DataPower API Gateway
onlyInput No The input message to convert. Specify the name of a variable in the API context. variableName.body, the message payload, represents the XML input to convert. The default value of the variable is message and message.body is the default input. string
DataPower API Gateway
onlyOutput No The output message to store the conversion result. Specify the name of a variable in the API context. variableName.body represents the result of conversion from XML format to JSON format. When the specified input message is the default message, the default output is message.body. Otherwise, when the input message is the variable my-message-variable, for example, the default output is my-message-variable.body.

The variable cannot be any read-only in the API context.

string
DataPower API Gateway
onlyConversion type No The conversion type that determines the target format of the output. The following options are available:
  • badgerFish: BadgerFish convention is used to determine the target conversion format of the output.
  • apicv5: apicv5 convention is used to determine the target conversion format of the output.
string

Examples

For example, the following simple XML object
<a>hello</a>
becomes
{ "a": { "$" : "hello" } }
The following XML object with an attribute
<a type="world">hello</a>
becomes
{ "a": { "$" : "hello", "@type" : "world" } }
For examples, see xml-to-json.