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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Operators Ignore Model Scores
  • 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

Trending

  • Why Your RAG Pipeline, Not Your LLM, Is Causing Hallucinations
  • The Agent Returned the Right Answer but Took the Wrong Path
  • Integration Testing With MongoDB in Java: Finding the @Sql Equivalent With Liquibase/Flyway or Jamolingo
  • Beyond Semantic Similarity: Designing Two-Stage RAG With Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

How to Use the 'Using' Operator in DataWeave

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

By 
Brian Labelle
·
Mar. 22, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Why Operators Ignore Model Scores
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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