2

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.

Tek_Datt
  • 25
  • 3

2 Answers2

3

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

George
  • 2,758
  • 12
  • 16
  • Thanks, George. Do you want your output type to be application/dw? I guess it's up to what the OP needs. – Dale Feb 24 '20 at 21:44
  • 1
    Yes @Dale, its up to the OP to decide what mime type to use for the output, I used `application/dw` because, IMHO, is the best format while I develop with the `Preview` turn on. Had I been the one doing this for production work I would most definitely switch to another format because `application/dw` is very slow during runtime. – George Feb 25 '20 at 00:09
0

%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

TheOtherGuy
  • 184
  • 2
  • 9