cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

To replace more than one character in string by looking up another field value

RaymanPrince
New Contributor II

I have a business requirement, where we have below JSON in that, have to update objectโ€™s field called TRANSLATION_ID by looking up another fields called VARIABLE_ID and VARIABLE_VALUE. Here if both VARIABLE_ID and VARIABLE_VALUE are not null then replace TRANSLATION_ID (say [A] to be replaceed with value in the variable value) with corresponding values present in VARIABLE_VALUE.

Note: Here currently we have max of 2 replacement values in VARIABLE_VALUE but we expect it can be more than 10.

Input json :
[
{
โ€œPHRASE_SEQโ€: 629,
โ€œPHRASE_IDโ€: โ€œ[Drug Name]โ€,
โ€œPLACEMENTโ€: โ€œ12โ€,
โ€œTRANSLATION_IDโ€: โ€œ[Drug Name]โ€,
โ€œVARIABLE_IDโ€: โ€œDrug Nameโ€,
โ€œVARIABLE_VALUEโ€: โ€œstr456โ€
},
{
โ€œPHRASE_SEQโ€: 630,
โ€œPHRASE_IDโ€: โ€œStore at [A]ยฐC to [B]ยฐCโ€,
โ€œPLACEMENTโ€: โ€œ13โ€,
โ€œTRANSLATION_IDโ€: โ€œStore at [A]ยฐC to [B]ยฐCโ€,
โ€œVARIABLE_IDโ€: โ€œA,Bโ€,
โ€œVARIABLE_VALUEโ€: โ€œ2,8โ€
}
]

Expected output would be:
[
{
โ€œPHRASE_SEQโ€: 629,
โ€œPHRASE_IDโ€: โ€œ[Drug Name]โ€,
โ€œPLACEMENTโ€: โ€œ12โ€,
โ€œTRANSLATION_IDโ€: โ€œstr456โ€,
โ€œVARIABLE_IDโ€: โ€œDrug Nameโ€,
โ€œVARIABLE_VALUEโ€: โ€œstr456โ€
},
{
โ€œPHRASE_SEQโ€: 630,
โ€œPHRASE_IDโ€: โ€œStore at [A]ยฐC to [B]ยฐCโ€,
โ€œPLACEMENTโ€: โ€œ13โ€,
โ€œTRANSLATION_IDโ€: โ€œStore at 2ยฐC to 8ยฐCโ€,
โ€œVARIABLE_IDโ€: โ€œA,Bโ€,
โ€œVARIABLE_VALUEโ€: โ€œ2,8โ€
}
]

5 REPLIES 5

Cele
New Contributor II

Hi @RaymanPrince

You can try to use the expression below in a mapper

$VARIABLE_ID != null && $VARIABLE_VALUE != null && $.extend({ 'TRANSLATION_ID': $VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),$TRANSLATION_ID) }) || $

RaymanPrince
New Contributor II

Hi @Cele

Many Thanks
The solution works as expected.

parthiban_jks
New Contributor II

Hi Cele,

Thanks you so much for the quick reply. Could you please explain about the reduce part in the above expression? I would be really helpful for us

Thanks,
Parthiban S

Cele
New Contributor II

HI @parthiban.jks

I will try to explain to you the best I can do

$VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),  $TRANSLATION_ID)

.reduce() is an array function which accepts 2 arguments, first is the callback function(arrow funciton) and the second is the initialValue, so to use the reduce youโ€™ll need an array, so what Iโ€™m doing is taking the $VARIABLE_ID and spliting by comma, so this will return an array of strings divided at the supplied separator(comma โ€œ,โ€).

The reducer walks through that array element-by-element,and at each step, using the currentValue of the array, finds that currentValue(โ€˜Aโ€™, โ€˜Bโ€™, โ€ฆ) in $TRANSLATION_ID and repalce it with value present in VARIABLE_VALUE consequently, what I mean with this is that Iโ€™m also splitting the $VARIABLE_VALUE by comma, this will also return an array, so I can access the value, which needs to be replace in $TRANSLATION_ID, with the index of the first array consequently

.reduce((accumulator, currentValue, currentIndex, arr) => , initialValue)

{
  "PHRASE_SEQ": 630,
  "PHRASE_ID": "Store at ยฐC to [Y]ยฐC",
  "PLACEMENT": "13",
  "TRANSLATION_ID": "Store at [A]ยฐC to [B]ยฐC",
  "VARIABLE_ID": "A,B",
  "VARIABLE_VALUE": "2,8"
}

$VARIABLE_ID.split(',')    -->   ['A', 'B'] - array
$VARIABLE_VALUE.split(',') -->   ['2', '8'] - array

$VARIABLE_ID.split(',').reduce((acc, cur, index) => acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]),  $TRANSLATION_ID)
                                    
 ['A', 'B'].reduce((acc, cur, index) =>  acc.replaceAll([cur], ['2', '8'][index]) ,  "Store at [A]ยฐC to [B]ยฐC"  )

With this syntax ['2', '8'][index] when index is 0 we will get the value '2',
in the next iteration the index will be increment by 1 so we will get the value '8'

In the first step(first iteration) the currentValue(cur) is โ€˜Aโ€™ and the currentIndex (index) is 0, initialValue = $TRANSLATION_ID or โ€œStore at [A]ยฐC to [B]ยฐCโ€, and accumulator = the result from previous calls or the initialValue(in the first iteration) also value that we end with. In the function we updating the accumulator with new value, so acc.replaceAll([cur], $VARIABLE_VALUE.split(',')[index]) in the first iteration accumulator(acc) = โ€œStore at [A]ยฐC to [B]ยฐCโ€, and replaceAll method returns a new string
with all matches of a currentValue [โ€˜Aโ€™], replaced by a value form $VARIABLE_VALUE โ€˜2โ€™. Because the index is 0 in the first iteration this syntax $VARIABLE_VALUE.split(',')[index] would give the value โ€˜2โ€™ from $VARIABLE_VALUE(array). In the second interation the index will be 1, currentValue โ€˜Bโ€™ and accumulator will be updated withvalue from the previous call, that is โ€œStore at 2ยฐC to [B]ยฐCโ€, and so on until the last element of the array is reached

I hope this will helps you to understand better about reduce function, if not let me know I will try to explain better

BR
Kristijan