Json2POJOJson2POJO

Apache Camel: JSON to POJO

Unmarshal and marshal JSON in your Camel routes using Jackson or Gson. Enterprise-ready integration patterns.

Generate POJOs First

Prerequisites

Add the camel-jackson dependency to your project:

<!-- Maven -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jackson</artifactId>
    <version>3.x.x</version>
</dependency>

Unmarshal JSON to POJO

from("direct:start")
    .unmarshal().json(JsonLibrary.Jackson, User.class)
    .process(exchange -> {
        User user = exchange.getIn().getBody(User.class);
        System.out.println("Name: " + user.getName());
    })
    .to("mock:result");

The unmarshal() DSL converts the JSON string in the message body to your POJO class.

Marshal POJO to JSON

from("direct:toJson")
    .process(exchange -> {
        User user = new User("John", 30);
        exchange.getIn().setBody(user);
    })
    .marshal().json(JsonLibrary.Jackson)
    .to("mock:jsonResult");

The marshal() DSL converts your POJO to a JSON string.

Custom Jackson Configuration

JacksonDataFormat jackson = new JacksonDataFormat();
jackson.setUnmarshalType(User.class);
jackson.setPrettyPrint(true);
jackson.setInclude("NON_NULL");

from("direct:custom")
    .unmarshal(jackson)
    .to("mock:result");

Jackson vs Gson in Camel

FeatureJacksonGson
Dependencycamel-jacksoncamel-gson
PerformanceFasterSlower
Enterprise UseLimited

Common Questions

How to handle nested JSON?

Create matching nested POJOs. Jackson automatically handles nested object mapping during unmarshal.

JSON from REST endpoint?

Use from("rest:get:/api/user").unmarshal().json() with camel-rest component.

Generate Your POJOs First

Create the Java classes you need for your Camel routes instantly.

Convert JSON to POJO