Tek_Datt
Tek_Datt

Reputation: 25

Mapping value only in DataWeave 2.0/Mule 4

I'm a bit of a rookie at Mule 4 and have been trying to figure out how to map only the value from an array. My array is:

[
  {
    "ssn": "999999991",
    "contributionCode": "1",
    "amt": -100.000000
  },
  {
    "ssn": "999999991",
    "contributionCode": "2",
    "amt": 1200.000000
  }
]

What I need to do is filter the array by contributionCode and ssn and return only the amount value. I've tried this and variations of it but I only get errors:

vars.sumAmounts filter ($.ssn == '999999991' and $.contributionCode == '1' ) map -> ({$.amt})

So the output I'm trying to get from the above would be only -100.000000. Sorry in advance if this seems like a basic question but anyone's help would be very much appreciated.

Upvotes: 2

Views: 261

Answers (2)

TheOtherGuy
TheOtherGuy

Reputation: 184

%dw 2.0 output application/json var data = [ { "ssn": "999999991", "contributionCode": "1", "amt": -100.000000 }, { "ssn": "999999991", "contributionCode": "2", "amt": 1200.000000 }

]

(data filter ($.ssn == '999999991' and $.contributionCode == '1' ))[0].amt

Upvotes: 0

user3078986
user3078986

Reputation:

@Tek_Datt here's two ways you can do it and it all depends whether you will be doing this over and over again.

This one is using your approach of searching with the filter function:

%dw 2.0
output application/dw
var data = [
  {
    "ssn": "999999991",
    "contributionCode": "1",
    "amt": -100.000000
  },
  {
    "ssn": "999999991",
    "contributionCode": "2",
    "amt": 1200.000000
  }
]
---
(data filter ($.ssn == "999999991" and $.contributionCode ~= 1)).amt[0]

This next one is more performant if you want to run multiple such searches per transformation:

%dw 2.0
output application/dw
var data = [
  {
    "ssn": "999999991",
    "contributionCode": "1",
    "amt": -100.000000
  },
  {
    "ssn": "999999991",
    "contributionCode": "2",
    "amt": 1200.000000
  }
] groupBy ($.ssn ++ "_" ++ $.contributionCode)
---
data["999999991_1"].amt[0]

Pick the one you like.

Upvotes: 3

Related Questions