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

> A subtle DataWeave gotcha: when you read keys from an object, you don't always get an Array<String> — you may get an Array<String | Key>, and that breaks equality checks. Here's why, and how to fix it.

- **Author:** Alex Martinez
- **Published:** Feb 5, 2025
- **Category:** Tutorials
- **Tags:** MuleSoft, DataWeave
- **Source:** https://prostdev.com/post/array-string-key-dataweave

---
## 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.

```dataweave title="transform.dwl"
%dw 2.0
output application/json
var arr1 = [
  "key" as Key,
  "string"
]
var arr2 = [
  "key",
  "string"
]
---
[arr1, arr2]
```

```json title="output.json"
[
  [
    "key",
    "string"
  ],
  [
    "key",
    "string"
  ]
]
```

> [!PLAYGROUND]
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript1)

So imagine my surprise when I tried to use a [distinctBy](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-distinctby) function with the above arrays and I'd still get the same two arrays in the output.

```dataweave title="transform.dwl"
%dw 2.0
output application/json
var arr1 = [
  "key" as Key,
  "string"
]
var arr2 = [
  "key",
  "string"
]
---
[arr1, arr2] distinctBy $
```

```json title="output.json"
[
  [
    "key",
    "string"
  ],
  [
    "key",
    "string"
  ]
]
```

> [!PLAYGROUND]
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript2)

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.

```dataweave title="transform.dwl"
%dw 2.0
output application/json
var arr1 = [
  "key" as Key,
  "string"
]
var arr2 = [
  "key",
  "string"
]
---
[arr1, arr2] map ((item) ->
  item map ($ as String)
) distinctBy $
```

```json title="output.json"
[
  [
    "key",
    "string"
  ]
]
```

> [!PLAYGROUND]
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript3)

### 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](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-distinctby) function itself. Like this:

```dataweave title="transform.dwl"
%dw 2.0
output application/json
var arr1 = [
  "key" as Key,
  "string"
]
var arr2 = [
  "key",
  "string"
]
---
[arr1, arr2] distinctBy ($ joinBy ",")
```

```json title="output.json"
[
  [
    "key",
    "string"
  ]
]
```

> [!PLAYGROUND]
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript4)

Since we're using the [joinBy](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-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

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

💬 Prost! 🍻

---

## FAQs

### Why does distinctBy fail to remove a duplicate array even though the JSON output looks identical?

Because the items inside the arrays are different types: one array is an `Array<String>` while the other is an `Array<String | Key>`. The JSON output renders both the same way, so you incorrectly see them as identical, but the type difference means `distinctBy` treats them as distinct and keeps both.

### What's the difference between an `Array<String>` and an `Array<String | Key>` in DataWeave?

In this post, an `Array<String>` is produced when no key was added to the array, while an `Array<String | Key>` results when you add keys and/or values and one of the items ends up coerced into a Key. Both can look the same in the JSON output, but the differing item types break equality checks between the arrays.

### How do I fix the type mismatch so distinctBy works correctly?

The first solution is to make sure any new items are of type String and not Key, so you are comparing two arrays of type `Array<String>`; the post does this by mapping each item with `item map ($ as String)` before `distinctBy $`, which correctly returns only one array.

### What if I can't change the type of the items before comparing them?

If you can't change the item types or it would take too many iterations, you can transform the items inside the `distinctBy` function itself using `distinctBy ($ joinBy ",")`. The `joinBy` function turns each array's values into a concatenated string (both become "key,string") and then compares those, so the arrays are seen as the same and only one is returned.