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
Please enter at least three characters to search
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Implement Linked Lists in Go
  • Recursive Feature Elimination in Practice
  • Text Clustering With Deepseek Reasoning
  • Personalized Product Recommendations in E-Commerce Using ML

Trending

  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Develop a Reverse Proxy With Caching in Go
  1. DZone
  2. Data Engineering
  3. Data
  4. Comparing Two Arrays Using Dataweave

Comparing Two Arrays Using Dataweave

One such thing we often encounter is comparing two arrays and finding the differences. Let's check out the different scenarios and see how we can achieve them.

By 
Raviteja Anumalasetty user avatar
Raviteja Anumalasetty
·
Sep. 21, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.0K Views

Join the DZone community and get the full member experience.

Join For Free

While Dataweave 2.0 provides several out-of-the-box functions to transform the data, sometimes we need to orchestrate more than one such function to achieve the desired result. One such thing we often encounter is comparing two arrays and finding out the differences. Let's check out the different scenarios and see how we can achieve them.

Scenario 1: Comparing Two Arrays With Identical Structure

In this case, both arrays will have the same fields. If we want to see the items which are present in array 1 but not in array 2, we can use the "--" function.

Array 1 - defined as variable 'list1' :

 
[
	{
		"city": "Rotterdam",
		"country": "Netherlands"
	},
	{
		"city": "Amsterdam",
		"country": "Netherlands"
	},
	{
		"city": "Utrecht",
		"country": "Netherlands"
	}
]


Array 2 - defined as variable 'list2':

 
[
	{
		"city": "Rotterdam",
		"country": "Netherlands"
	},
	{
		"city": "Amsterdam",
		"country": "Netherlands"
	},
	{
		"city": "Eindhoven",
		"country": "Netherlands"
	}
]


Items present in list1 but not in list2:

 
vars.list1 -- vars.list2


The output of the above operation:

 
[
  {
    "city": "Utrecht",
    "country": "Netherlands"
  }
]


While the above scenario can be done easily with the default Dataweave operator, let's dive into the complex situation of comparing dissimilar arrays.

Scenario 2: Comparing Two Arrays With Different Structures

Array 1 - defined as variable 'list3':

 
[
	{
		"city": "Rotterdam",
		"country": "Netherlands"
	},
	{
		"city": "Amsterdam",
		"country": "Netherlands"
	},
	{
		"city": "Utrecht",
		"country": "Netherlands"
	}
]


Array 2 - defined as variable - 'list4':

 
[
	{
		"city": "Rotterdam",
		"country": "Netherlands",
		"province": "South Holland"
	},
	{
		"city": "Amsterdam",
		"country": "Netherlands",
		"province": "North Holland"
	},
	{
		"city": "Eindhoven",
		"country": "Netherlands",
		"province": "North Brabant"
	}
]


As we can see, the variable list4 has an extra field, 'province', which is not present in the variable list3. So, it won't be possible to compare both arrays with the default functions. 

In this case, below code snippet could help us to compare based on the identical fields between the two arrays:

 
vars.list3 -- (vars.list4 map {city: $.city, country: $.country})


The output of the above step:

JSON
 
[
	{
		"city": "Utrecht",
		"country": "Netherlands"
	}
]


If we want to find the elements in list4 but not in list3,

 
((list4 map {city: $.city, country: $.country}) -- list3) map (obj, ind) -> (list4 filter ($.city == obj.city and $.country == obj.country))[0]


Which gives the output as:

 
[
  {
    "city": "Eindhoven",
    "country": "Netherlands",
    "province": "North Brabant"
  }
]


Let's see what we are doing with the above expression:

 
(list4 map {city: $.city, country: $.country})


In the above step, we are trying to convert list4 into the same structure as list3.

 
((list4 map {city: $.city, country: $.country}) -- list3)


After converting the arrays into the same structure, we are finding out items that are in list4 but not in list3.

 
((list4 map {city: $.city, country: $.country}) -- list3) map (obj, ind) -> (list4 filter ($.city == obj.city and $.country == obj.country))[0]


After finding out the elements which are in list4 but not in list3, we are using that list to match with the elements in list4, so we get the complete structure of list4 only for the different items.

Data structure

Opinions expressed by DZone contributors are their own.

Related

  • How to Implement Linked Lists in Go
  • Recursive Feature Elimination in Practice
  • Text Clustering With Deepseek Reasoning
  • Personalized Product Recommendations in E-Commerce Using ML

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!