Transforming from XML to JSON

Camel provides an XML JSON Component that converts your data back and forth between XML and JSON in a single step, without an intermediate Java object representation. It provides a Camel Data Format that allows you to marshal (XML to JSON), or unmarshal (JSON to XML) your data.

This recipe will show you how to use the XML JSON Component to convert from XML to JSON, and back.

Getting ready

Java code for this recipe is located in the org.camelcookbook.transformation.xmljson package. Spring XML files are located under src/main/resources/META-INF/spring and prefixed with xmljson.

To use Camel's XML JSON Component, you need to add a dependency element for the camel-xmljson library, which provides an implementation for the XML JSON Data Format.

Add the following to the dependencies section of your Maven POM:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-xmljson</artifactId>
  <version>${camel-version}</version>
</dependency>

How to do it...

Reference the xmljson Data Format within your route, choosing the marshal (XML to JSON), or unmarshal (JSON to XML) statement, as appropriate:

In the XML DSL, this is written as follows:

<route>
  <from uri="direct:marshal"/>
  <marshal>
    <xmljson/>
  </marshal>
  <to uri="mock:marshalResult"/>
</route>

In the Java DSL, this same route is expressed as:

from("direct:marshal")
  .marshal().xmljson()
  .to("mock:marshalResult");

How it works...

Using the Camel XML JSON Component simplifies translating your data between XML and JSON, making it convenient to use when you are dealing with REST endpoints. The XML JSON Data Format wraps around the Json-lib library, which provides the core translation capabilities, eliminating more boilerplate code from your integration flows.

There's more...

You may need to configure XML JSON if you want to fine-tune the output of your transformation. For example, consider the following JSON:

[{"@category":"PROGRAMMING","title":{"@lang":"en","#text":"Apache Camel Developer's Cookbook"},"author":["Scott Cranton","Jakub Korab"],"year":"2013","price":"49.99"}]

This will be converted as follows, by default, which may not be exactly what you want (notice the <a> and <e> elements):

<?xml version="1.0" encoding="UTF-8"?>
<a>
  <e category="PROGRAMMING">
    <author>
      <e>Scott Cranton</e>
      <e>Jakub Korab</e>
    </author>
    <price>49.99</price>
    <title lang="en">Apache Camel Developer's Cookbook</title>
    <year>2013</year>
  </e>
</a>

To configure XML JSON to use <bookstore> as the root element instead of <a>, use <book> for the individual elements instead of <e>, and expand the multiple author values to use a sequence of <author> elements, you would need to tune the configuration of the Data Format before referencing it in your route.

In the XML DSL, the definition of the Data Format and the route that uses it is written as follows:

<dataFormats>
  <xmljson id="myXmlJson"
           rootName="bookstore"
           elementName="book"
           expandableProperties="author author"/>
</dataFormats>

<route>
  <from uri="direct:unmarshalBookstore"/>
  <unmarshal ref="myXmlJson"/>
  <to uri="mock:unmarshalResult"/>
</route>

In the Java DSL, the same thing is expressed as:

XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setRootName("bookstore");
xmlJsonFormat.setElementName("book");
xmlJsonFormat.setExpandableProperties(
    Arrays.asList("author", "author"));

from("direct:unmarshalBookstore")
  .unmarshal(xmlJsonFormat)
  .to("mock:unmarshalBookstoreResult");

This will result in the previous JSON being unmarshalled as follows:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="PROGRAMMING">
    <author>Scott Cranton</author>
    <author>Jakub Korab</author>
    <price>49.99</price>
    <title lang="en">Apache Camel Developer's Cookbook</title>
    <year>2013</year>
  </book>
</bookstore>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.28.108