Hello friends, 

In this article I want show you the uses of two very common dataweave functions splitBy and joinBy. 

These two functions are very useful while doing transformation with Array and Strings. 

Here, we will see how we can use these dataweave functions converting data from String to Array and vice versa.

splitBy

Splits a string into a string array based on a value of input or matches part of that input string. It filters out the matching part from the returned array.

A string or a Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array.

splitBy(String, Regex): Array<String>

Example 1:
------------------------------
Input
Hello World
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/java
---
"Hello World" splitBy(/s/)
------------------------------
Output
[Hello, World]

splitBy(String, String): Array<String>

Example 2:
------------------------------
Input
Manish Kumar
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/java
---
payload splitBy(" ")
------------------------------
Output
[Manish, Kumar]

The regex/separator can match any character in the input. splitBy performs the opposite operation of joinBy.


joinBy

joinBy function Merges an array into a single string value and uses the provided string as a separator between each item in the list. 

You can use joinBy Dataweave 2.0 function to convert array to string.

Example 3:
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/json
---
{ "joinName" : ["Manish","Kumar"] joinBy "-" }
------------------------------
Output
{
"joinName": "Manish-Kumar"
}

joinBy performs the opposite task of splitBy.

Happy Learning 🙂

By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

2 thoughts on “splitBy and joinBy function dataweave2”
  1. HI sir ,
    joinby you example very usefully but my question is very simple for you i think ,
    Example 3:
    ——————————
    Dataweave 2.0 Expression
    ——————————
    %dw 2.0
    output application/json

    { “joinName” : [“Manish”,”Kumar”] joinBy “-” }
    ——————————
    Output
    {
    “joinName”: “Manish-Kumar”
    }

    — if incase arrays are multiple is there then how to write expression, i mean
    i have one input like this
    input:
    [“Manish”,”Kumar”],
    [“first”,”last”], [“sai”, “name”]

    1. Hi Sai,
      You can try below:
      %dw 2.0
      output application/json

      { “joinName” :
      [ [“Manish”,”Kumar”],[“first”,”last”],[“sai”,”name”] ] map ((item, index) -> item joinBy “-“)
      }

      Output:
      {
      “joinName”: [
      “Manish-Kumar”,
      “first-last”,
      “sai-name”
      ]
      }
      Hope it helps!!

Leave a Reply

Your email address will not be published. Required fields are marked *