0

I have this code in my Mulesoft application doing something like a filter which I dont understand. This looks like Java to me

        custListInqResponse: {
            (payload.custBasic filter ($.taxId == flowVars.varLoanAcctTaxId) map {
                custPermId: $.custPermId
            })
        }

I will like to write the same logic in Python. Any ideas how to write this? An interpretation of the above code will also be appreciated

aled
  • 21,330
  • 3
  • 27
  • 34
Baba
  • 2,059
  • 8
  • 48
  • 81

1 Answers1

0

The snippet shared is not Java. It is a DataWeave 1.x script from a Mule 3.x application. DataWeave is a functional language used in Mule applications for data transformation in a Transform component.

There is not enough context of the snippet to understand if the output is a JSON, XML, Java or some other format supported by DataWeave. I'll assume it is JSON.

payload.custBasic has to be some array. The filter operation is used to keep only the elements that comply with a condition. The condition is ($.taxId == flowVars.varLoanAcctTaxId), which means for each element of the input array, take the field taxId and compare if it is equal to the value of the flow variable varLoanAcctTaxId. A flow variable is a kind of variable used in Mule 3 applications. It should have been set in a previous operation than this DataWeave transformation. The resulting filtered list of elements is then transformed into a list of elements that only contains the attribute custPermId.

Finally the resulting list of transformed objects is assigned to a parent object with attribute (or key) custListInqResponse. This seems to mean that the key-values inside the transformation are expanded into the parent, because using parenthesis around a list of key-values has that effect in DataWeave. Also having just an array inside an object with no keys would be an error.

For example with input payload:

{
  "custBasic": [
    { "taxId": 1, "custPermId": 123},
    { "taxId": 2, "custPermId": 456},
    { "taxId": 1, "custPermId": 789}
  ]
}

and flowVar.varLoanAcctTaxId == 1 the output would be:

{
  "custListInqResponse": {
    "custPermId": 123,
    "custPermId": 789
  }
}

In Python a similar concept is using a filter() function.

aled
  • 21,330
  • 3
  • 27
  • 34
  • Do you know how I can write this in Python performing a search on taxId: 888888 ? – Baba Jun 02 '22 at 14:50
  • Please add the actual input and output to your original question to inform answers. – aled Jun 02 '22 at 14:55
  • Added a reference to Python filter() function. – aled Jun 02 '22 at 14:58
  • I did. The input is the json file which you have and I also added. The output is the example you showed which has the two custPermId – Baba Jun 02 '22 at 14:59
  • The output you provided above can never be a valid json file because of the repetition of custPermId. I think custPermId should be a list – Baba Jun 02 '22 at 17:07
  • The script that you provided will produce that JSON with duplicate keys. Any chance your script has `duplicateKeyAsArray=true` in its header section? FYI [JSON can "technically" have duplicate keys and still be valid](https://stackoverflow.com/a/23195243/10946202) (not recommended though) – Harshank Bansal Jun 02 '22 at 17:27
  • 1
    As @HarshankBansal mentioned, that is the output of the script you shared with the input I created to try to match the script. Questions like this should include the inputs and expected/actual outputs. I believe you tried to add the expected output to my answer. It should be in the question. I don't see the input/outputs in the question. Unless there is some other change the snippet creates that output. If it is valid or not depends on the JSON implementation. DataWeave allows it for example. – aled Jun 02 '22 at 17:54