Open Source API Gateway
2. Enterprise Integration Patterns

json2Xml

Version

Converts a JSON message body to XML. A body that is not JSON passes through unchanged. The result is UTF-8 encoded, begins with an XML prolog, and the Content-Type is set to application/xml. If the body cannot be converted, the exchange is aborted.

Given an XML Schema, the document follows the schema instead: the single key of the JSON object names the root element, and the fields are written in the order and namespace the schema declares.

 json2Xml:
   [ root: <element name> ]
   [ array: <element name> ]       # default: array
   [ item: <element name> ]        # default: item

 json2Xml:
   xmlSchema: <url or path>       # replaces root, array and item
 

Example Configuration

<pre><code>
api:
port: 2000
flow:
- json2Xml:
root: order
</code></pre>
<p>See the tutorial tutorials/xml/10-JSON-to-XML.yaml.</p>
<pre><code>
 api:
   port: 2000
   flow:
     - json2Xml:
         root: order
 </code></pre>
 <p>See the tutorial tutorials/xml/10-JSON-to-XML.yaml.</p>

Syntax

json2Xml:
array: <string>
item: <string>
root: <string>
xmlSchema: <string>
json2Xml:
  array: <string>
  item: <string>
  root: <string>
  xmlSchema: <string>

Attributes

NameRequiredDefaultDescriptionExamples
arrayfalsearrayXML element name wrapped around every JSON array, nested ones included.items
itemfalseitemXML element name used for each item of a JSON array.entry
rootfalsederived from the JSONName of the single XML root element the converted document is wrapped in. When set, every result is wrapped in an element with this name. When unset, the name is derived from the document: a single-property object uses that property as the root, a top-level array uses the array element name, and any other object or value is wrapped in root.order
xmlSchemafalse-An XML Schema (XSD) that drives the mapping: a URL or a file path, relative to the configuration file. Its imports and includes are loaded too. The JSON must be an object with a single key naming a global element of the schema, e.g. {"books": {...}}. Fields are written in the order the schema declares them and in their namespace, an array becomes repeated elements, @-prefixed keys become attributes, $value becomes the element's text, and null becomes xsi:nil="true". Invalid JSON, or a root key the schema does not declare, is rejected with 400 in the request flow and 500 in the response flow. Cannot be combined with root, array or item.books.xsd

Can be used in