How to enrich data by inserting json in other json in ESB

741 Views Asked by At

I am using Camel APIs for routing in my project with ESB and have the below requirement for a specific problem:-

There is a web service called using the form tag URI which takes in the request as JSON and responds the POST call with JSON data. I need to include the request JSON data as one of the fields inside my response JSON. I am searching for a possible way to enrich my message. I could do it by writing the transformation logic in a java class. But Constraint is that I can not use separate Java class for doing data transformation. I have to do it via configuration inside camelContext using tags. Please suggest.

2

There are 2 best solutions below

0
On

You can read entire Json body as a tree JsonNode body = mapper.readTree(input); and after that enrich it with your construction. For example, if you have next json body:

{"valueList": {
    "key": "1",
    "value": "2"
  }
}

you can enrich it with this piece of code:

  ObjectMapper mapper = new ObjectMapper();
  ObjectNode rootNode = mapper.createObjectNode();
  ArrayNode headersList = mapper.createArrayNode();
  rootNode.set("headersList", headersList);
  rootNode.putPOJO("valueList", body.get("valueList"));
0
On

I think you need to make a look at setBody method. Please check here.

...
<setBody>
<simple>
{
"message" : "${body}"
}
</simple>
</setBody>
...