Splitting a Json Array with Camel

2.8k Views Asked by At

I have a camel route that takes a String object of json, calls a bean to strip out a JSONArray of players.

from("direct:players").routeId("player_route")
    .bean(BootstrapStaticParser.class,"getPlayersList")
    .split(body())
    .to("direct:aws");

However after I call:

.split(body())

What should be a json string justs into a LinkedHashMap but I require to maintain the json format to insert the data into AWS DDB.

Logging to show the issue

Before

Exchange[ExchangePattern: InOnly, BodyType: net.minidev.json.JSONArray, Body: [{"id":1,"photo":"48844.jpg","web_name":"Player1","team_code":3},{"id":2,"photo":"11334.jpg","web_name":"Player2","team_code":3},{"id":3,"photo":"98980.jpg","web_name":"Player3","team_code":3},{"id":4,"photo":"51507.jpg","web_name":"Player4","team_code":3},...]

After

Exchange[ExchangePattern: InOnly, BodyType: java.util.LinkedHashMap, Body: {id=1, photo=48844.jpg, player=Player1, team_code=3}]

Any help would be appreciated.

2

There are 2 best solutions below

0
On

This post is old, but as I don't like to let a question unanswered, I am trying to here.

Camel 2.17: The collate function iterates the message body and groups the data into sub lists of specified size. This can be used with the Splitter EIP to split a message body and group/batch the splitted sub message into a group of N sub lists. This method works similar to the collate method in Groovy.

How to use it:

<split ...>
    <!-- supposed an ArrayList of Json  -->        
    <simple>${collate(3)}</simple>
    
    <log message="Splited Body: ${body}" />
    <log message="Split dataSplitIndex: ${headers.dataSplitIndex}, isLast: ${exchangeProperty.CamelSplitComplete}" />
    
</split>
0
On

According to the documentation of Camel Jsonpath the message body of a splitted JSON document part is a Map.

However, it says that with Camel 2.20 or newer you can use jsonpathWriteAsString to get a JSON string instead of the Map.

.split().jsonpathWriteAsString("$.yourJsonPath")

For earlier versions of Camel you have to use Camel JSON data format to marshal the message body from the Map to a JSON String.