Json string split and add addition object in the body using XML DSL in camel

975 views Asked by At

In one of my use case i need to split the inbound json message into multiple message based on the array list that is available in the message.

for example, in the below message split to be taken place based "actualData" array list.

{   
  "information": {
    "name": "ObjectName",
    "type": "messageType"   
   },   
  "actualData": [
    {
      "msg": "message-1"
    },
    {
      "msg": "message-2"
    }   
  ] 
}

Once the split is done, need to add the "information" object in each message before send it to next route. this work needs to be done in XML DSL itself and not using any java processor or java code.

Expected result needs to be like this:

split message:1

{
  "information": {
    "name": "ObjectName",
    "type": "messageType"
  },
  "actualData": 
    {
      "msg": "message-1"
    }
}

Split message:2

{
  "information": {
    "name": "ObjectName",
    "type": "messageType"
  },
  "actualData": 
    {
      "msg": "message-2"
    }
}

Problem statement:

I am able to split the message based on "actualData" array object but when i try to add the additional object in the final exchange body, I am getting some problem to convert the json object to string using jsonpath.

below is the route which will be called for each message.

<route id="split_message">
<from uri="direct:split_message_onebyone"/>
<log message="Sending message body to split message one by one : ${body} "/>
<convertBodyTo type="java.lang.String"/>
<setProperty name="information">
    <jsonpath resultType="String" writeAsString="true">$.information</jsonpath>
</setProperty>
<log message="printing property after string message body to split message one by one : ${exchangeProperty.information}} "/>
<split streaming="true">
    <jsonpath>$.actualData</jsonpath>
    <marshal> <custom ref="jack" /> </marshal>
    <setBody>
        <simple>{{"information" : ${exchangeProperty.information}}, "actualData" : ${body}, } </simple>
    </setBody>
    <convertBodyTo type="java.lang.String"/>
</split>
<log message="split end one by one ${body}"/>
</route>    

The approach that i attempted here was, before actual split occurs, get the "information" object from the original message and store into exchange property with the name of "information" and then after split occured, refer this property from exchange and prepare the body in which create a json with both "information" + "actualData" object.

everything works as expected but the problem here is, when the "information" object is getting stored in property field, its not stored as a string rather its storing as below.

{
    name: "ObjectName",
    type: "messageType"   
}

expected to store as below.

{
    "name": "ObjectName",
    "type": "messageType"   
 }

because of this problem, final body message not become json string. Can someone please give a hand to solve this? Thanks in advance. I am using camel 3.4 version.

2

There are 2 answers

0
user20405190 On

Do marshalling after your response instead converting to String

1
Akos On

Try Java DSL: .split().jsonpathWriteAsString("$")

or YAML:

  • split: jsonpath: writeAsString: true expression: "$"