Krish
Krish

Reputation: 4242

How to get hashmap particular key value in mule dataweave

I am new for mule and i am trying to fetch below hashmap memberId value in dataweave but below code getting compile time error, Can some one help me to how do we fetch memebrId value alone from below hasmap?.

hashmap:

{
  "0":{
    "key":"auth",
    "value":true
  },
  "1":{
    "key":"memebrId",
    "value":"ESC0099123"
  },
  "2":{
    "key":"name",
    "value":"Ramakrishna"
  }
}

Code which I tried:

(payload pluck $ filter ($.key == "memebrId"))[0].value

Upvotes: 0

Views: 2054

Answers (2)

Salim Khan
Salim Khan

Reputation: 4303

you could do something like this

%dw 2.0
output application/json
---
(payload pluck $ filter ($.key == "memebrId"))[0].value

enter image description here

Upvotes: 3

Harshank Bansal
Harshank Bansal

Reputation: 3262

You can take all values of the object as an array using valuesOf, and then filter the array to get the object containing memberId which will give you an array of single object related to memberId.

%dw 2.0
output application/json
---
valuesOf(payload)[?($.key == 'memebrId')][0].value

The [?($.key == 'memebrId')] part is an alternative selector to filter you can use to shorten the overall expression, and avoid some extra braces. Since you are new to this, I will give the expanded version

(valuesOf(payload) filter ((item) -> item.key == 'memebrId'))[0].value

image from https://developer.mulesoft.com/learn/dataweave/

Upvotes: 3

Related Questions