Skip to content

Commit 97db4d0

Browse files
feat(blog): add playground links for distinctBy examples in DataWeave article
1 parent fcd03d9 commit 97db4d0

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/content/blog/array-string-key-dataweave.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ I didn't think much about it at first, since I would -incorrectly- see all the a
2323
2424
![DataWeave script with arr1 keyed and arr2 strings, both outputting the same JSON arrays](../../assets/blog/array-string-key-dataweave-1.png)
2525

26+
> [!PLAYGROUND]
27+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript1)
28+
2629
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.
2730

2831
![distinctBy applied to the two arrays still returns both, since their item types differ](../../assets/blog/array-string-key-dataweave-2.png)
2932

33+
> [!PLAYGROUND]
34+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript2)
35+
3036
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!
3137

3238
It was until one of those million times finally led me to see the array types were actually different 🤦
@@ -39,12 +45,18 @@ There are different solutions for this problem. One would be to make sure any ne
3945

4046
![Casting items to String before distinctBy now collapses the output to one array](../../assets/blog/array-string-key-dataweave-3.png)
4147

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

4453
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:
4554

4655
![distinctBy with a joinBy criteria collapses the two arrays into one in the output](../../assets/blog/array-string-key-dataweave-4.png)
4756

57+
> [!PLAYGROUND]
58+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript4)
59+
4860
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.
4961

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

0 commit comments

Comments
 (0)