Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use the 'Using' Operator in DataWeave

DZone 's Guide to

How to Use the 'Using' Operator in DataWeave

See how to use the 'using' operator in DataWeave, the MuleSoft mapping tool.

· Integration Zone ·
Free Resource

I recently had a business problem that required using the "using" operator in DataWeave. I noticed there wasn't a lot of documentation on it, so this will be a brief overview of my problem and how the using operator helped me.

The using operator allows us to use a variable in a scope, for example, within a map. This is useful when you need to aggregate a subset of elements in an array. Let's say we have an array with the following elements:

{"letterCountArray": [
{"letter": "A","count": 5},
{"letter": "B","count": 3},
{"letter": "C","count": 6},
{"letter": "D","count": 5},
{"letter": "E","count": 7}]}

Our requirement is to display the letter and count for A, B and C.

We can write the following DataWeave...

result: payload.letterCountArray filter $.letter == "A" or $.letter == "B" 
or $.letter == "C" map {
(letterA: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "A",
(letterB: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "B",
(letterC: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "C"
}

...which will give the result:

{
  "result": [
    {
      "letterA": {
        "Letter": "A",
        "Count": 5
      }
    },
    {
      "letterB": {
        "Letter": "B",
        "Count": 3
      }
    },
    {
      "letterC": {
        "Letter": "C",
        "Count": 6
      }
    }
  ]
}

Now the user says, "I love your program, but I would like to have the sum of E and D counts." A simple solution is to use the using operator. You can create a function to sum the count of D and E and put that into a variable. You can use that variable to display the count within the map...

%function getCountDandE(letterCountArray) (
   letterCountArray filter $.letter == "D" or $.letter == "E" map {
   count:  $.count}.count reduce ($$ + $)
)
---
result: payload.letterCountArrayfilter
           $.letter == "A" or $.letter == "B" or $.letter == "C" or $.letter == "D" map
    using (countOfDandE = getCountDandE(payload.letterCountArray)){

(letterA: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "A",

(letterB: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "B",

(letterC: {
   Letter: $.letter,
   Count: $.count
}) when $.letter == "C",

(letterDandE: {
   Letter: "D+E",
   Count: countOfDandE
}) when $.letter == "D"

}

...which will now give the result:

{
  "result": [
   {
      "letterA": {
        "Letter": "A",
        "Count": 5
      }
    },
    {
      "letterB": {
        "Letter": "B",
        "Count": 3
      }
    },
    {
      "letterC": {
        "Letter": "C",
        "Count": 6
      }
    },
    {
      "letterDandE": {
        "Letter": "D+E",
        "Count": 12
      }
    }
  ]
}

As you can see, the using operator is a powerful feature, but remember, as Uncle Ben from Spiderman said, "With great power comes great responsibility."

Topics:
mulesoft ,integration ,api ,mule developer ,mule ,esb ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}