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

  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Amazon OpenSearch Vector Search Explained for RAG Systems
  • Production-Grade RAG: Why Vector Search Isn't Enough (and How Hybrid Search Fills the Gaps)

Trending

  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • From ETL to Lakeflow: Shifting to a Declarative Data Paradigm
  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
12.7K 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

  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Amazon OpenSearch Vector Search Explained for RAG Systems
  • Production-Grade RAG: Why Vector Search Isn't Enough (and How Hybrid Search Fills the Gaps)

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