2

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")
dur
  • 15,689
  • 25
  • 79
  • 125
Aadi
  • 1,131
  • 2
  • 16
  • 32

1 Answers1

5

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()
dur
  • 15,689
  • 25
  • 79
  • 125
Alexey Yakunin
  • 1,743
  • 1
  • 19
  • 21