Skip to main content

[

{

  "Order1": [

      {

      "Case1": "abcd",

      "Case2": "def"

      }

],

  "Order2": [

       {

       "Case3": "abcd",

      }

]

}

]

 

Hello friends,

This is my sample json array. how to replace "abcd" to "xyz" without altering the other data?

 

Thanks

4 answers
  1. May 21, 2020, 11:10 AM

    Hi @Usersend 27​, you can use the below recursive function:

    %dw 1.0

    %output application/json

    %var in = [

    {

    "Order1": [

    {

    "Case1": "abcd",

    "Case2": "def"

    }

    ],

    "Order2": [

    {

    "Case3": "abcd"

    }

    ]

    }

    ]

    %function replaceValue(e,r,v)

    e match {

    :array -> $ map (value,index) -> replaceValue(value,r,v),

    :object -> $ mapObject (value,key) ->

    (key): v when (value == r)

    otherwise replaceValue(value,r,v),

    default -> e

    }

    ---

    replaceValue(in,"abcd","xyz")

    output:

    [

    {

    "Order1": [

    {

    "Case1": "xyz",

    "Case2": "def"

    }

    ],

    "Order2": [

    {

    "Case3": "xyz"

    }

    ]

    }

    ]

     

    Regards,

    Karolina

0/9000