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

  • Securing AI Agents in Production: A Developer’s Guide To Prompt Injection, RAG Leaks, and Tool Abuse
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough

Trending

  • Function Calling: Responses API vs Chat Completions
  • Bolting an LLM Onto a System That Can't Afford to Be Wrong
  • Why AI Agents Need a Shared Enterprise Ontology
  • Reusable Operational Playbooks With Agent Skills

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.

  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
·
Sep. 21, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.1K 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

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

  • Securing AI Agents in Production: A Developer’s Guide To Prompt Injection, RAG Leaks, and Tool Abuse
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Threat Modeling Context-Aware AI: A STRIDE Walkthrough

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