Search code examples
muledataweavemule4

How to iterate over the string using data weave 1.0 and data weave 2.0?


I'm new to dataweave and trying to transform the array and iterate over the "||" values

Input:

[
    {
        "card":"VISA$$0.0||MASTER$$140.0"
    },
    {
        "card":"VISA$$0.0||MASTER$$147.0"
    }
]

The DataWeave code that I tried:

%dw 2.0
output application/json
---
"CardList":payload map (data,index) ->
{
    (data.card splitBy "||" map {
        "sur": $
    })
}

Expected response :

{
  "cardList": [

        {
          "card": "VISA$$0.0"
        },
        {
          "card": "MASTER$$140.0"
        },
        {
          "card": "VISA$$0.0"
        },
        {
          "card": "MASTER$$147.0"
        }
  ]
}

Someone one could you please assist me here on mule 3 and 4.

thanks in advance.


Solution

  • Try as below - iterating through splitBy values (||)

    %dw 2.0
    output application/json
    ---
    "CardList": flatten(payload map 
    ( ($.card splitBy  "||")  map(item,index) -> 
    {
        card : item
    }))