Skip to main content

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"

    }

]

2 answers
  1. Sep 19, 2023, 9:58 PM

    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:

    Please find the below: %dw 2.Note: You can try the read function DataWeave in Studio with JSON.

     

    Thanks,

    Manish Kumar Yadav

    MuleSoft Forum Moderator

0/9000