2

I want to check if the value present in the YAML list.

I have product.yaml

intGrp:
  - "A"
  - "CD"
  - "EF"
  - "ABC"
  - "CDEF"

From transform message I want to check

If (intGrp contains payload.myvalue) this else that

Tried

%dw 2.0
var prop = Mule::p('intGrp')
output application/json
---
{
    a: prop contains ("A")
}

But that doesn't solve my problem. Because I want to do an exact string match. i.e if I give a: prop contains ("AB") I should get a false as there is no product as "AB".

Any help would be highly appreciated. Thank you

Neethu Raj
  • 37
  • 1
  • 6

1 Answers1

0

The problem is that the YAML array is interpreted as a comma separated string in the property. The contains() function works differently in strings than in array. In strings it searches for a matching substring, hence 'AB' returns true. You could convert the string it back to an array using the splitBy() DataWeave function. I'm showing both side by side to highlight the difference:

%dw 2.0
var prop = Mule::p('intGrp') 
var propArray = Mule::p('intGrp') splitBy ',' 
output application/json
---
{
    raw: prop,
    array: propArray,
    a: propArray contains ("A"),
    ab: propArray contains ("AB")
}

The output is:

{
  "raw": "A,CD,EF,ABC,CDEF",
  "array": [
    "A",
    "CD",
    "EF",
    "ABC",
    "CDEF"
  ],
  "a": true,
  "ab": false
}

Note that if any of the entries contains a comma it will be split too.

aled
  • 21,330
  • 3
  • 27
  • 34