Reputation: 959
I have a Camel route in which I'm using the Content Enricher EIP. So my route looks like this:
AggregationStrategy aggregationStrategy = new TestAggregationStrategy();
from("timer:myTimer?repeatCount=1")
.setBody(simple("{\"channel\": \"orderbook\", \"market\": \"BTC/USD\", \"type\": \"update\", \"data\": {\"time\": 1626139758.986094, \"checksum\": 195176195, \"bids\": [[32965.0, 0.0], [32962.0, 3.4015]], \"asks\": [], \"action\": \"update\"}}"))
.setHeader(MongoDbConstants.CRITERIA, constant(Filters.eq("market", "BTC/USD")))
.enrich("mongodb:mongo?database=dev&collection=order&operation=findOneByQuery", aggregationStrategy)
.log("Test: ${body}");
And inside TestAggregationStrategy I have two exchanges, the original and the resource.
public class TestAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange original, Exchange resource) {
System.out.println("Original: " + original.getMessage().getBody().toString());
String bidsJsonQuery = "$.data.bids";
DocumentContext updateContext = JsonPath.parse(original.getMessage().getBody().toString());
List<String> updateJsonString = updateContext.read(bidsJsonQuery);
System.out.println(resource.getMessage().getBody());
//ArrayList test = (ArrayList) resource.getMessage().getBody(List.class);
Object test = resource.getMessage().getBody();
System.out.println("Test: " + test);
System.out.println("Resource: " + resource.getMessage().getBody());
return resource;
}
}
I can get to the message content with resource.getMessage().getBody(), which returns an object of some kind. If I throw this into a println(), it prints the Mongo document. When I examine the resource exchange with the debugger, it looks like this:
exchange > in > body ("Document")
Which contains an array of hash maps. In this example, one of the hash maps has a key of "bids" with a value of an array list.
Are you able to access these values directly? The array list? I've been experimenting and I can't get past the getBody().
Upvotes: 0
Views: 203
Reputation: 7005
Your code (Jsonpath query and conversion) is OK, but "bids" is not a List<String>
In the message body you set "bids" is a List<List<Double>>
.
When I take your Json String I can extract the "bids" in pure Java like this
String jsonString = "{\"channel\": \"orderbook\", \"market\": \"BTC/USD\", \"type\": \"update\", \"data\": {\"time\": 1626139758.986094, \"checksum\": 195176195, \"bids\": [[32965.0, 0.0], [32962.0, 3.4015]], \"asks\": [], \"action\": \"update\"}}";
String bidsJsonQuery = "$.data.bids";
DocumentContext updateContext = JsonPath.parse(jsonString);
List<List<Double>> bids = updateContext.read(bidsJsonQuery);
for (List<Double> bid : bids) {
for (Double singleBid : bid) {
logger.info("{}", singleBid);
}
}
The output of the logger is
32965.0
0.0
32962.0
3.4015
By the way: in your code you parse the original
message body. In the text you write about the resource
body. Is this a mistake in your code?
Upvotes: 2