Hi,
I need to convert a key-value pair which is in String to JSON using Dataweave. Please help on this.
Input String: "[{key=1, value=ABC},{key=2, value=XYZ}]"
Output:
[
{
"key": "1",
"value": "ABC"
},
{
"key": "2",
"value": "XYZ"
}
]
Please find the below:
%dw 2.0
output application/json
---
"[{key=1, value=ABC},{key=2, value=XYZ}]" splitBy ("},{") map ((item) -> item replace /(\[\{)|(\])|(\})/ with "" splitBy (",") map ((in) -> {
"$((trim(in) splitBy "=")[0])": (trim(in) splitBy "=")[1]
})) map {
($)
}
Out:
[
{
"key": "1",
"value": "ABC"
},
{
"key": "2",
"value": "XYZ"
}
]
FYI:
Note: You can try the read function DataWeave in Studio with JSON.
Thanks,
Manish Kumar Yadav
MuleSoft Forum Moderator