2
votes

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.

2

2 Answers

3
votes

@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.

0
votes

%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