1

I am trying to implement Camel CBR based on JSONPath filter expressions.

Body (JSON structure) is as follows :

{
    "orderId": "315973",
    "status": "Complete",
    "entity": {
        ...
    }
 }

My route is as follows :

// Unimportant part
from("direct:in")
      .streamCaching()
      .setHeader(Exchange.HTTP_METHOD, constant("POST"))
      .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
      .setHeader("Authorization", simple(AUTH_TOKEN))
          .to(SOME_HTTP_ENDPOINT_1).bean(ResultIDExtractorBean.class)
          .to(SOME_HTTP_ENDPOINT_2).bean(OrderIDExtractorBean.class)
          .to(SOME_HTTP_ENDPOINT_3)
             .choice()
// Important part
                  .when().jsonpath("$[?(@.status == 'Complete')]" , false)
                      .to("mock:complete")
                  .when().jsonpath("$[?(@.status == 'In Progress')]" , false)
                       .to("mock:in_progress")
                  .otherwise()
                        .to("mock:error").stop()
                 .end();

Two issues I am facing :

1) If am getting exception :org.apache.camel.ExpressionEvaluationException: com.jayway.jsonpath.InvalidPathException: Filter: [?] can not be applied to primitives. I can surpress this exception, but the second (more crucial) issue is not solved.

2) Message is not routed based on "status".

This predicate expression works however:

 .when(PredicateBuilder.isEqualTo(
      ExpressionBuilder.languageExpression("jsonpath", "$.status"), 
      ExpressionBuilder.constantExpression("In Progress")))

I'd like to know how to leverage JSON path filter expressions in Camel CBR. Thanks.

P.S. camel/camel-jsonpath versions are 2.17.0

fg78nc
  • 4,774
  • 3
  • 19
  • 32
  • Your CBR code as well as the jsonpath expressions are ok. I can copy/paste it into a Camel route test and it works as expected. However, I use a ProducerTemplate to send a message and I send the JSON body as String (without "entity"). – burki Oct 17 '18 at 13:16
  • I tried to send JSON body as a file with template. It does not work for me somehow with `jsonpath()`, but it works with `PredicateBuilder`. – fg78nc Oct 17 '18 at 13:34
  • make sure the json you parsing is actually valid, in my case that was the issue – Luca Mattia Ferrari Aug 08 '22 at 15:02

0 Answers0