Aadi
Aadi

Reputation: 1161

How to check that a key exists with jsonpath in a Camel route?

This JSON object below needs to be checked if a key exists or not. If key exists and value is empty then I want set TH as default language.

How to do that in camel route?

{ "languagePreference":"" }
 //set default value of language preference as TH
.setHeader("languagePreference").jsonpath("$.languagePreference")

Upvotes: 2

Views: 2967

Answers (1)

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

You can use the suppressExceptions flag

.setHeader("languagePreference").jsonpath("$.languagePreference", true)

This will not throw an exception if the key is missing. After that, you can check for a value in the header, and then you can assign the desired value if header is empty (There are many ways to check the header value).

        //.choice().when(PredicateBuilder.or(header("languagePreference").isNull() , header("languagePreference").isEqualTo("")))
        .choice().when().simple("${header.languagePreference} == null || ${header.languagePreference} == ''")
           .setHeader("languagePreference").constant("TH")
        .end()

Upvotes: 6

Related Questions