Apache Camel: XML vs. JSON
XML 1.0 was released in 1998. Ten years later when I started my career XML usage was at its peak. SOAP, XSLT, XSD, XQuery, XPath, WSDL, UDDI and a zillion others were popular.
Apache Camel, released in 2007, supported most of these standards. But things were about to shift towards JSON. Does Camel support this format today just as well?
Introduction
I have worked with so many XML-related technologies that it has always felt like home. Still, over the years, I have had to acknowledge that the XML data format was somewhat over-engineered (I’m looking at you, namespace!), and that it was not flexible enough for programming (for example, Ant).
Since 2010, the IT world has slowly shifted towards JSON and other technologies. JSON is much easier to parse in programming languages than XML and is very well supported by most web front ends because it originates from JavaScript.
However, JSON hasn’t always been easy to work with. A trailing comma can crash parsing, and comments are nowhere to be found. JSON5 mitigates many of these shortcomings but is unfortunately not widely supported.
And most of all, working in the integration field, it felt like we finally had things like validation, transformation, and querying ironed out in XML, but then we had to start all over again with JSON…
Camel
From the beginning, Camel routes could be written in XML. XML schema validation was introduced in version 1.1, and XSLT was introduced in version 1.3. Support for JSON related technologies like JSON Schema came much later, in version 2.20, and support for JSONata came in version 3.5.
In the meantime, you could say that XML and JSON technologies are mostly on par with each other in Camel 4. However, I missed most of the arrival of the JSON counterparts of XML. Is there an overview of the necessary technologies for different tasks, such as validation or transformation?
This has also to do with the sheer size of Apache Camel. With more than 350 components available, it’s easy to overlook something. So, let’s create an overview:
You can check every component in detail in the components reference.
Simple: XML and JSON
Besides specific components and data formats, Camel’s simple language, also has support for XML and JSON:
- Pretty prints XML or JSON:
${prettyBody}
${pretty(exp)}2. JSONPath
Using JSONPath within simple:
${jsonpath(exp)}
${jsonpath(input,exp)}3. JQ
Using JQ within simple
${jq(exp)}
${jq(input,exp)}4. XPath
Using XPath within simple
${xpath(exp)}
${xpath(input,exp)}This makes simple languages pretty good at templating XML or JSON, for example, using the setBody method. However, you can also use specialized templating languages such as Freemarker, Thymeleaf or Mustache for it.
Mapping
Mapping XML and JSON can be done with transformers or serializers, however there is also a graphical mapper created by Kaoto. Currently it only supports XML, but JSON support is underway:
Conclusion
The conclusion is that XML and JSON support are not that different from each other these days. Both can used to great solid integrations, no matter what data format your endpoints use. Hopefully, this blog gave you a brief overview of the possibilities and which components you can choose.
