Search code examples
jsonata

Return True if Array Contains a Specific String - JSONata


Is there a way in JSONata to have a function return TRUE if it finds a specific string within a provided array? For example I have an array of colors:

const myArray = [red,blue,green,pink]

I am trying to figure out an expression that would search that array for "blue" and return true if it finds the value.

On the JSONata documentation, I found a function called $boolean(arg)that I think I would need to use but I'm not sure how to implement it. The documentation shows an argument type option as "array: contains a member that casts to true", but I can't really tell how to implement it.

Would it be as simple as $boolean(myArray, "blue")?


Solution

  • The in operator is what you need. See https://docs.jsonata.org/comparison-operators#in-inclusion

    In your case, the expression "blue" in myArray will return true. See https://try.jsonata.org/r0q7GnSOh