Handle Json Data in Apache Camel

Posted By : Hrishabh Mishra | 30-Sep-2020

ERP

Loading...

To handle JSON data, we use the Jackson library. The Jackson library is a small library in Apache Camel that deals with the JSON data format with an xml mapper to unmarshal a json payload into java object and marshal java object into a json payload.

eg.

1

2

3

from("file: path of file").

unmarshal().jacksonxml().

to("file: path of file");

How to add Jackson Library into the project:-

By Adding Maven dependency in your project we add jackson Library.

1

2

3

4

5

6

<dependency>

<groupId>org.apache.camel</groupId>

<artifactId>camel-jacksonxml</artifactId>

<version>x.x.x</version>

<!-- use the same version as your Camel core version -->

</dependency>

This dependency is added into pom.xml in your project and automatically pulls the jackson library or .jar file into your project.

Marshalling the Json Data:-

By using the Java Pojo classes we map the header of a specific json message i.e json file.

eg.

1

2

3

4

5

6

7

8

9

10

11

12

13

@JsonPropertyOrder({"roll_no", "name", "class"})

public class Students {

@JsonProperty("roll_no")

private String roll_no;

private String name;

private int class;

@JsonIgnore

private String note;

}

Configuration of Pojo Class:-

1

2

JacksonDataFormat format = new JacksonDataFormat(Students.class);

format.setAllowJmsType(true);

we get this after marshalling the java object into json payload.

1

2

3

4

5

6

{

"roll_no":1,

"name":"Warren Buffet",

"class":"4"

}

Unmarshalling the json Data:-

1

2

3

4

5

6

7

8

9

10

11

12

13

@JsonPropertyOrder({"roll_no", "name", "class"})

public class Students {

@JsonProperty("roll_no")

private String roll_no;

private String name;

private int class;

@JsonIgnore

private String note;

}

Configuration -

1

2

3

4

5

6

7

8

9

10

11

JacksonDataFormat format = new JacksonDataFormat(Students.class);

format.setAllowJmsType(true);

from("file: path of file")

.process(exchange->

{

Students student=exchange.getIn.getbody(Students.class)

// Here "student" is the java object of json file which contain the body of json file.

});

Conclusion:-

Jackson library is an excellent way to parse or unmarshall the json data into java objects and Marshal the java object into json payload. It also maps the specific header of json payload using Pojo classes. This is the best way to handle json data in apache Camel using Jackson library. By using annotation ,it is easy to modify the header of the json payload.

We provide end-to-end ERP development and integration services to drive organizational growth by simplifying HR/CRM operations and automating compliance and payroll processes. Our team uses open-source tools and ERP platforms to custom build feature-rich enterprise applications from the ground up to address varied business needs.