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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

Trending

  • What is Microsoft Fabric for Azure Cloud (Beyond the Buzz) and How It Competes with Snowflake and Databricks
  • Managing Encrypted Aurora DAS Over Kinesis With AWS SDK
  • Agile’s Quarter-Century Crisis
  • Building AI Agents With Python, LangChain, and GPT APIs
  1. DZone
  2. Data Engineering
  3. Data
  4. Mule 3.9: Dataweave 1.0 Conditional Filtering

Mule 3.9: Dataweave 1.0 Conditional Filtering

In this article, I will explain how to leverage the Dataweave language on performing data filtering.

By 
Enrico Rafols Dela Cruz user avatar
Enrico Rafols Dela Cruz
·
Sep. 02, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
42.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will explain how to leverage the Dataweave Language on performing data filtering. For instance, we need to prepare a payload for a Salesforce Upsert Operation where the initial records (Contacts) came from a CSV file and it should be enriched to include the lookup ID (Account) from Salesforce.

CONTACTS (CSV): Stored in our payload.

Account Name,Email,Home Phone,Mobile,Fax,Mailing Address,Status
Enrico R. Dela Cruz,[email protected],14344,343434,34343,Maninla PH,Active
Lebron James,[email protected],5445,5434,5434,LA USA,Active
Kevin Durant,[email protected],2123,3221,1233,USA,Active
Kevin Love,[email protected],7689,7676,766467,USA,Inactive

ACCOUNT IDS (Salesforce): Assuming we already have the needed lookup ID as a Collection Data Type, which came from Salesforce Connector (Query). This value is stored in a variable lookupAccountIds.

[
  {
    Account_Name="Enrico R. Dela Cruz",
    Account_Id="AC00001"
  },
  {
   Account_Name="Lebron James",
   Account_Id="AC00002"
  },
  {
   Account_Name="Kevin Love",
   Account_Id="AC00003"
  }
]

Dataweave Script: This script filters the payload that contains the list of contacts before converting to an Object Data Type (application/java). Only contacts with an account name that already exists in Salesforce and has an active status are included on the output payload for Salesforce Upsert operation.

%dw 1.0
%output application/java
%var registeredAccountNames = 
     flowVars.lookupAccountIds.Account_Name
%function getAccount(item){
id: (flowVars.lookupAccountIds filter $.Account_Name == item."Account Name")[0].Account_Id,
activeFlag: true when item."Status" == "Active" otherwise false
}
---
payload filter ((registeredAccountNames contains $."Account Name") and $."Status" == "Active") map {
Account_Id: getAccount($).id,
Account_Name: $."Account Name",
Fax_Number: $."Fax",
Home_Phone_Number: $."Home Phone",
Mobile_Number: $."Mobile",
Mailing_Address: $."Mailing Address",
Active: getAccount($).activeFlag
}

I'm using a filter operator, which allows me to impart conditions of what records should be included on the map operation. Then, inside the map operation, to get the corresponding Account_Id from lookupAccountIds for a particular Contact, I created a function that filters the variable lookupAccountIds by the Account Name from the payload.

For more details about the Dataweave Operators, check this out: https://docs.mulesoft.com/mule-user-guide/v/3.9/dataweave-reference-documentation#operators

Testing:

Image title

Payload (computing) Contacts (Apple) Data Types Data (computing) Filter (software) Operator (extension) Record (computer science) Leverage (statistics) CSV

Opinions expressed by DZone contributors are their own.

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: