1

I would like to filter an array based on another array values. I would like to filter the input array based on key value "KNVP-PARVW" is in the list of ["BP,SH,PY"]

here is the input:

[{
    "KNVP-KUNNR": "100098",
    "KNVP-VTWEG": "A1",
    "KNVP-PARVW": "BP",
    "KNVP-PARZA": "000",
    "KNVP-KUNN2": "200115",
    "KNVP-DEFPA": ""
  },
  {
    "KNVP-KUNNR": "100098",
    "KNVP-VTWEG": "A1",
    "KNVP-PARVW": "SH",
    "KNVP-PARZA": "001",
    "KNVP-KUNN2": "200115",
    "KNVP-DEFPA": ""
  },
  {
    "KNVP-KUNNR": "100098",
    "KNVP-VTWEG": "A1",
    "KNVP-PARVW": "ZR",
    "KNVP-PARZA": "000",
    "KNVP-KUNN2": "256",
    "KNVP-DEFPA": ""
  }]

and here is my dataweave code:

%dw 2.0
var relationList=["BP,SH,PY"]
output application/json
---
payload filter (  relationList contains  $."KNVP-PARVW" )

2 Answers 2

4

While ["BP,SH,PY"] is a list, it has only one element which is a string. To use contains() as you intend, it has to be a list of the valid values for $."KNVP-PARVW":

%dw 2.0
var relationList=["BP","SH","PY"]
output application/json
---
payload filter (  relationList contains  $."KNVP-PARVW" )

Output:

[
  {
    "KNVP-KUNNR": "100098",
    "KNVP-VTWEG": "A1",
    "KNVP-PARVW": "BP",
    "KNVP-PARZA": "000",
    "KNVP-KUNN2": "200115",
    "KNVP-DEFPA": ""
  },
  {
    "KNVP-KUNNR": "100098",
    "KNVP-VTWEG": "A1",
    "KNVP-PARVW": "SH",
    "KNVP-PARZA": "001",
    "KNVP-KUNN2": "200115",
    "KNVP-DEFPA": ""
  }
]
Sign up to request clarification or add additional context in comments.

Comments

-1

Got it the var relationList should be like below

var relationList="BP,SH,PY"

1 Comment

That is a bad solution. You are using contains() with a string, so if $."KNVP-PARVW" == "P,S" then it would be true because it is a valid substring of relationList.

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.