top of page

Why is my Array<String> not filtering correctly? It may be an Array<String | Key> instead 🤷


Watch this video for detailed explanations ⬆️

In this post:





The Problem


Ok so here's what was happening to me and I didn't know why.


I was doing a bunch of transformations and at one point I'd add some keys and/or values into an array. So, some of the arrays were of type Array<String> (because no key was added here) while some of them where Array<Key | String>


I didn't think much about it at first, since I would -incorrectly- see all the arrays in the JSON output were of type Array<String>. Like this:


ℹ️ Note The item in line 4 was coerced into a Key for demonstration purposes.

Code snippet and output side by side. Code defines two arrays, arr1 and arr2, with strings "key" and "string". Output shows nested arrays.


So imagine my surprise when I tried to use a distinctBy function with the above arrays and I'd still get the same two arrays in the output.


Code snippet using DataWeave to create JSON objects with arrays and distinctBy function, displayed in a text editor interface.


I started losing my mind trying to troubleshoot the problem a million different ways because I didn't notice the items in the arrays were of different types!


It was until one of those million times finally led me to see the array types were actually different 🤦



Possible Solutions


Solution 1

There are different solutions for this problem. One would be to make sure any new items are of type String and not Key. This completely removes the issue since now you'd be comparing two arrays of type Array<String>, which correctly returns only one array in the output.


Code editor showing a script for JSON output. Variables arr1 and arr2 contain "key" and "string”. The command distinctBy is used.


Solution 2

If you can't change the type of the items or it would take too many iterations to do, another thing you can do is to transform the items inside the distinctBy function itself. Like this:


A code snippet in black text displays a distinctBy function on two arrays with JSON format output. OUTPUT panel shows key and string results.


Since we're using the joinBy function, we're transforming the values of arr1 and arr2 into concatenated strings and THEN comparing them. Both arr1 and arr2 would be "key,string", which makes these values the same.





I hope this post brings you more DataWeave knowledge or helps you fix some code :D


Let me know your thoughts in the comments 👇


Subscribe to receive notifications as soon as new content is published ✨


💬 Prost! 🍻



8 Comments


kenzyken
kenzyken
Mar 02

High-quality finishes often reflect the expertise of construction professionals. Furthermore, multi finish helps create smooth surfaces suitable for decoration. Proper technique ensures consistency and durability. The College of Contract Management provides guidance through structured courses.

Like

I found do my matlab homework useful while practicing MATLAB problems,


 especially for understanding logic errors and improving my coding approach.

Like

In supply chain governance, construction line helps promote transparency by enabling buyers to assess environmental, health, and safety policies among suppliers. This supports responsible procurement. Verified data informs decisions. UNICCM explains governance value.

Edited
Like

nyla harper
nyla harper
Nov 27, 2025

If you have ever asked “what is a diploma”? The answer is simple- it’s a qualification that helps individuals gain professional skill and knowledge in a specific field. Unlike a degree, which often takes several years to complete, a diploma can be earned in a short time, making it an efficient option for those looking to enter the workforce quickly. This makes them a popular choice for students who want to build experience without spending years in higher education.

Like

Nick Johns
Nick Johns
Sep 22, 2025

This helped me a lot. Looking forward to more posts like this. Please write an article about Best Neck Pain Clinics In Valencia.

Like

Join our mailing list

Thanks for subscribing!

  • Youtube
  • GitHub
  • LinkedIn
bottom of page