12 October 2013

Camel has many data formats including support for JSON marshalling. In fact Camel supports three different libraries for JSON marshalling: Jackson, Xstream, and GSON.

This post will show you how to use Jackson with Talend Studio. To use the Camel Jackson data format you need to add the camel-jackson-alldep-2.10.4.jar dependency to your route using the cConfig component. The version number should match whatever version of camel you are using. In Talend 5.3.1 we use Camel 2.10.4.

To unmarshal from JSON to POJO use the cJavaDSLProcessor with this simple command (note the leading “.”, this is Camel Java DSL)

.unmarshal().json(org.apache.camel.model.dataformat.JsonLibrary.Jackson, beans.JazzResponse.class)

The first parameter, is the fully qualified Class name of the Jackson library. It can be shortened to just JsonLbirary.Jackson if you add an import statement in your cConfig.

import org.apache.camel.model.dataformat.JsonLibrary;

The second parameter is the fully qualified name of your POJO class.

To marshal from POJO to JSON is even easier. Just add the cJavaDSLProcessor as shown below:

.marshal().json(JsonLibrary.Jackson)

Keep in mind that Camel has lots of syntactic sugar available in its DSL. If you find yourself writing lots of code to use utility libraries take a second look in the documentation.

Here is a sample JSON input file used in a Talend Studio route demonstrating JSON marshalling and unmarshalling with Jackson. Import it with Import Items into the Studio Mediation perspective.

JSON marshalling with Jackson route