JSON to XML

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

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 json-to-xml.

About

The JSON to XML policy uses a simple convention, based on BadgerFish, to convert your API context payload from JSON to XML. The policy expects the JSON input to be in the same format as the BadgerFish convention, so the structure can be rebuilt in XML. No additional configuration is required. For more information about the BadgerFish convention, see BadgerFish.
Note: The JSON to XML policy does convert the JSON structure { "a" : "hello" } (which is not BadgerFish convention) into <a>hello</a>.

Use the IBM® API Connect API Designer 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 a JSON-formatted request into an XML-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 JSON to XML policy must be parsed data. One way to produce parsed data is to use a Parse policy before a JSON to XML 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 json-to-xml.

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 JSON 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 JSON format to XML 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:
  • None: No conversion of the output takes place.
  • badgerFish: BadgerFish convention is used to determine the target conversion format of the output.
string
Root XML Element Name Yes The root element name of the resultant XML document. This property is used only if the input JSON document is not hierarchical and has more than one uppermost level property, or if the Always output the root element check box is selected.

The default value is json.

string
Always output the root element Yes Select this check box if you always want the policy to output the root element, even if it is not required to make the XML document well formed.

The default value is false.

boolean
Element name for JSON array elements No The XML element name to be used for JSON array elements. string

Examples

For example, the following simple JSON object
{ "a": { "$" : "hello" } }
becomes
<a>hello</a>
The following JSON object with an attribute
{ "a": { "$" : "hello", "@type" : "world" } }
becomes
<a type="world">hello</a>