1

I have a string like this (which looks like a map)

{key1=value1;key2=value2;key3=value3;...keyn=valuen;}

which I want to either convert to a java object or as a JSON payload like this:

{"key1" : "value1","key2" : "value2","key3" : "value3",..."keyn" : "valuen"}

Is there a mule way to do it ? I am trying to avoid writing a custom java class for this problem. Something which data weave can help?

2 Answers 2

1

Use this to get map

%dw 1.0
%output application/json
---
{((payload replace /[{}]/ with "" splitBy ";")  map using (data = $ splitBy "="){
    (data[0]) : data[1]
})}

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! Works very well ;)
1

I have used following in dataweave to manipulate string.

%dw 1.0
%output application/json
---
(payload replace /[{}]/ with "" splitBy ";")  map using (data = $ splitBy "=")   {
    (data[0]) : data[1]
}

Input data :-

"{key1=value1;key2=value2;key3=value3;keyn=valuen}"

Output :-

[
  {
    "key1": "value1"
  },
  {
    "key2": "value2"
  },
  {
    "key3": "value3"
  },
  {
    "keyn": "valuen"
  }
]

Hope this helps..

1 Comment

thanks @anupambhusari, I really dont want to put them in a array. Is it possible to transform as a single map, like shown in my sample ? - {"key1" : "value1","key2" : "value2","key3" : "value3","keyn" : "valuen"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.