DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Trending

  • Workflows vs AI Agents vs Multi-Agent Systems: A Practical Guide for Developers
  • How to Build a Local LLM Agent to Automate Work List Generation from Monthly Reports (With Jira Integration)
  • AI Assessments Are Everywhere
  • From "Vibe Coding" to Production: Setting Up an Evals Loop for Claude Agents

How to Use the 'Using' Operator in DataWeave

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

By 
Brian Labelle user avatar
Brian Labelle
·
Mar. 22, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
25.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook