Skip to main content

Hi,

I would like to filter an array of keys from an object. Please help to achieve this using Dataweave 2.

 

Input:

{

"name":"Mule",

"email":"test@user.com"

"phone":57673564,

"zip":43534,

"gender":"male"

}

 

From the above example I would like to filter only keys matching with my array (ex: ["email","phone","zip"])

 

Output

 

{

"email":"test@user.com"

"phone":57673564,

"zip":43534

}

3 answers
  1. Feb 29, 2020, 10:34 AM

    Hi @Ramkumar K​ , you can try the below:

    %dw 2.0

    output application/json

    var in = [

    {

    "name": "Mule",

    "email": "test@user.com",

    "phone": 57673564,

    "zip": 43534,

    "gender": "male"

    },

    {

    "name": "Mule",

    "email": "test@user.com",

    "phone": 57673564,

    "zip": 43534,

    "gender": "male"

    }

    ]

    var filterArray = ["email","phone","zip"]

    ---

    in map (obj,indexOfObj) ->

    obj mapObject (value,key) ->

    ((key): value) if (filterArray contains key as String )

    output:

    [

    {

    "email": "test@user.com",

    "phone": 57673564,

    "zip": 43534

    },

    {

    "email": "test@user.com",

    "phone": 57673564,

    "zip": 43534

    }

    ]

0/9000