Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 344. XML JSon DataFormat (deprecated)

Available as of Camel version 2.10

Camel already supports a number of data formats to perform XML and JSON-related conversions, but all of them require a POJO either as an input (for marshalling) or produce a POJO as output (for unmarshalling). This data format provides the capability to convert from XML to JSON and vice-versa directly, without stepping through intermediate POJOs.

This data format leverages the Json-lib library to achieve direct conversion. In this context, XML is considered the high-level format, while JSON is the low-level format. Hence, the marshal/unmarshal semantics are assigned as follows:

  • marshalling ⇒ converting from XML to JSON
  • unmarshalling ⇒ converting from JSON to XML.

344.1. Options

The XML JSon dataformat supports 13 options which are listed below.

NameDefaultJava TypeDescription




Sets the encoding. Used for unmarshalling (JSON to XML conversion).




Specifies the name of the XML elements representing each array element. Used for unmarshalling (JSON to XML conversion).




Specifies the name of the top-level XML element. Used for unmarshalling (JSON to XML conversion). For example, when converting 1, 2, 3, it will be output by default as 123. By setting this option or rootName, you can alter the name of element 'a'.




Determines whether the resulting JSON will start off with a top-most element whose name matches the XML root element. Used for marshalling (XML to JSon conversion). If disabled, XML string 12 turns into 'x: '1', 'y': '2' . Otherwise, it turns into 'a': 'x: '1', 'y': '2' .




Flag to be tolerant to incomplete namespace prefixes. Used for unmarshalling (JSON to XML conversion). In most cases, json-lib automatically changes this flag at runtime to match the processing.




Specifies the name of the top-level element. Used for unmarshalling (JSON to XML conversion). If not set, json-lib will use arrayName or objectName (default value: 'o', at the current time it is not configurable in this data format). If set to 'root', the JSON string 'x': 'value1', 'y' : 'value2' would turn into value1value2, otherwise the 'root' element would be named 'o'.




Determines whether white spaces between XML elements will be regarded as text values or disregarded. Used for marshalling (XML to JSon conversion).




Determines whether leading and trailing white spaces will be omitted from String values. Used for marshalling (XML to JSon conversion).




Signals whether namespaces should be ignored. By default they will be added to the JSON output using xmlns elements. Used for marshalling (XML to JSon conversion).




Removes the namespace prefixes from XML qualified elements, so that the resulting JSON string does not contain them. Used for marshalling (XML to JSon conversion).




With expandable properties, JSON array elements are converted to XML as a sequence of repetitive XML elements with the local name equal to the JSON key, for example: number: 1,2,3 , normally converted to: 123 (where e can be modified by setting elementName), would instead translate to 123, if number is set as an expandable property Used for unmarshalling (JSON to XML conversion).




Adds type hints to the resulting XML to aid conversion back to JSON. Used for unmarshalling (JSON to XML conversion).




Whether the data format should set the Content-Type header with the type from the data format if the data format is capable of doing so. For example application/xml for data formats marshalling to XML, or application/json for data formats marshalling to JSon etc.

344.2. Basic Usage with Java DSL

344.2.1. Explicitly instantiating the data format

Just instantiate the XmlJsonDataFormat from package org.apache.camel.dataformat.xmljson. Make sure you have installed the camel-xmljson feature (if running on OSGi) or that you’ve included camel-xmljson-7.3.jar and its transitive dependencies in your classpath. Example initialization with a default configuration:

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();

To tune the behaviour of the data format as per the options above, use the appropriate setters:

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));

Once you’ve instantiated the data format, the next step is to actually use the it from within the marshal() and unmarshal() DSL elements:

// from XML to JSON
// from JSON to XML

344.2.2. Defining the data format in-line

Alternatively, you can define the data format inline by using the xmljson() DSL element:

// from XML to JSON - inline dataformat
// from JSON to XML - inline dataformat

If you wish, you can even pass in a Map<String, String> to the inline methods to provide custom options:

Map<String, String> xmlJsonOptions = new HashMap<String, String>();
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ENCODING, "UTF-8");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ROOT_NAME, "newRoot");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.SKIP_NAMESPACES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.REMOVE_NAMESPACE_PREFIXES, "true");
xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.EXPANDABLE_PROPERTIES, "d e");

// from XML to JSON - inline dataformat w/ options
// form JSON to XML - inline dataformat w/ options

344.3. Basic usage with Spring or Blueprint DSL

Within the <dataFormats> block, simply configure an xmljson element with unique IDs:

    <xmljson id="xmljson"/>
    <xmljson id="xmljsonWithOptions" forceTopLevelObject="true" trimSpaces="true" rootName="newRoot" skipNamespaces="true"
             removeNamespacePrefixes="true" expandableProperties="d e"/>

Then you simply refer to the data format object within your <marshal /> and <unmarshal /> DSLs:

    <from uri="direct:marshal"/>
    <marshal ref="xmljson"/>
    <to uri="mock:json" />

    <from uri="direct:unmarshalWithOptions"/>
    <unmarshal ref="xmljsonWithOptions"/>
    <to uri="mock:xmlWithOptions"/>

Enabling XML DSL autocompletion for this component is easy: just refer to the appropriate Schema locations, depending on whether you’re using Spring or Blueprint DSL. Remember that this data format is available from Camel 2.10 onwards, so only schemas from that version onwards will include these new XML elements and attributes.

The syntax with Blueprint is identical to that of the Spring DSL. Just ensure the correct namespaces and schemaLocations are in use.

344.4. Namespace mappings

XML has namespaces to fully qualify elements and attributes; JSON doesn’t. You need to take this into account when performing XML-JSON conversions.

To bridge the gap, Json-lib has an option to bind namespace declarations in the form of prefixes and namespace URIs to XML output elements while unmarshalling (i.e. converting from JSON to XML). For example, provided the following JSON string:

{ "pref1:a": "value1", "pref2:b": "value2" }

you can ask Json-lib to output namespace declarations on elements pref1:a and pref2:b to bind the prefixes pref1 and pref2 to specific namespace URIs.

To use this feature, simply create XmlJsonDataFormat.NamespacesPerElementMapping objects and add them to the namespaceMappings option (which is a List).

The XmlJsonDataFormat.NamespacesPerElementMapping holds an element name and a Map of [prefix ⇒ namespace URI]. To facilitate mapping multiple prefixes and namespace URIs, the NamespacesPerElementMapping(String element, String pipeSeparatedMappings) constructor takes a String-based pipe-separated sequence of [prefix, namespaceURI] pairs in the following way: |ns2||ns3||.

In order to define a default namespace, just leave the corresponding key field empty: |ns1||||.

Binding namespace declarations to an element name = empty string will attach those namespaces to the root element.

The full code would look like that:

XmlJsonDataFormat namespacesFormat = new XmlJsonDataFormat();
List<XmlJsonDataFormat.NamespacesPerElementMapping> namespaces = new ArrayList<XmlJsonDataFormat.NamespacesPerElementMapping>();
namespaces.add(new XmlJsonDataFormat.
                       NamespacesPerElementMapping("", "|ns1||||"));
namespaces.add(new XmlJsonDataFormat.
                       NamespacesPerElementMapping("surname", "|ns2||" +

And you can achieve the same in Spring DSL.

344.4.1. Example

Using the namespace bindings in the Java snippet above on the following JSON string:

{ "name": "Raul", "surname": "Kripalani", "f": true, "g": null}


Would yield the following XML:

<person xmlns="" xmlns:ns1="">
    <g null="true"/>
    <surname xmlns:ns2="" xmlns:ns3="">Kripalani</surname>

Remember that the JSON spec defines a JSON object as follows:

An object is an unordered set of name/value pairs. […​].

That’s why the elements are in a different order in the output XML.

344.5. Dependencies

To use the XmlJson dataformat in your camel routes you need to add the following dependency to your pom:

  <!-- Use the same version as camel-core, but remember that this component is only available from 2.10 onwards -->

<!-- And also XOM must be included. XOM cannot be included by default due to an incompatible
license with ASF; so add this manually -->

344.6. See Also