I have to compare/exactly match one String against an array of String.
Input:
{
"country": "India",
"countries": "India,Russia,USA"
}
Output:
If country matches from the List present in countries then Return True, if not then return False.
It usually makes sense to separate your problem, in this case in two parts
For 1 you can use something like splitBy. And then for 2 you can use contains.
So, you can do something like:
%dw 2.0
output application/json
fun check(p) = do {
var countries = splitBy(p.countries, ",")
---
contains(countries, p.country)
}
---
check(payload)
payload.countries contains payload.country
contains could be used to check whether the substring (country) is present in available string (list of countries).
However, It would return true even if an incomplete substring like "Ind" is provided for country as technically the substring is present in the available countries string.
So, splitBy could be used to split the available string (countries) to individual substrings and checked with the country substring.
%dw 2.0
output application/json
---
payload.countries splitBy(",") contains payload.country