0

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.

3 Answers 3

1

It usually makes sense to separate your problem, in this case in two parts

  1. Parse the list of countries as a list
  2. Check whether your country is present or not

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Suppose "country : 'Ind' ", then it should match.. so will your code work in this case??
0

%dw 2.0 output application/json

payload.countries contains payload.country

2 Comments

Answers should provide an explanation, not just code.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.