Mule-Newbie
Mule-Newbie

Reputation: 103

How to convert string to json object using DataWeave

How do I convert below input string (which will be retrieved from config.yaml properties file) to required json object using Dataweave?

Input:

"ABC:123,DEF:456,GHI:789"

Required Output:

{
    "ABC":"123",
    "DEF":"456",
    "GHI":"789"
}

Upvotes: 3

Views: 2814

Answers (2)

Salim Khan
Salim Khan

Reputation: 4303

%dw 2.0
output application/json
---

{(("ABC:123,DEF:456,GHI:789" splitBy ",")  map {
    ({ (($ splitBy ":")[0]): (($ splitBy ":")[1])})

})}

Upvotes: 1

sudhish_s
sudhish_s

Reputation: 628

splitBy and reduce can be used like below

("ABC:123,DEF:456,GHI:789" splitBy ",") reduce ((item, accumulator={}) -> 
    accumulator ++ 
    do
    {   
        var items = item splitBy  ":"
        ---
        (items[0]): items[1]
    })

Upvotes: 3

Related Questions