dataweave

Convert "AAAAAABBBBCCCDD" to "A6B4C3D2" in Dataweave


working on a DataWeave transformation and need some help converting a string to a compressed format. Here’s the requirement:

Input :

{ "string":"AAAAAABBBBCCCDD" }

Desired Output:

{ "string":A6B4C3D2 }

In the output, I want to count consecutive characters and represent them in a compressed format.


Solution

  • You can get the desired output by grouping the continuous same characters and then concatenate each character with its counting. check below DataWeave Script

    here input is

    {
        "inputString": "AAAAAABBBBCCCDD"
    }
    

    solution to get desired output is:

    %dw 2.0
    output application/json
    var test = payload.inputString groupBy ($)
    ---
    {
    "String": (keysOf(test) map($ ++ sizeOf(test[$]))) joinBy ""
    }
    

    below is the DataWeave playground screen shot enter image description here