Search code examples
azure-data-factoryazure-synapse

How to update Nth Element of an array


Is it possible to update/replace the nth element of an array

input =[1,2,3,4]

Update 3rd element from 3 to 5

Output=[1,2,5,4]]


Solution

  • In ADF, self-referencing variables is not supported. So, use a temporary variable first and then reassign that temporary variable value to required one.

    If you only want to do it in ADF and not in dataflow, follow the below approach.

    I have created two array variables named x with values [1,2,3,4] and temp.

    I have created two parameters for nth element and updated value.

    enter image description here

    X value at start:

    enter image description here

    I have used the below expression to get the updated array.

    @createArray(take(variables('x'),sub(pipeline().parameters.nth,1)),pipeline().parameters.value,skip(variables('x'),pipeline().parameters.nth))
    

    enter image description here

    This will give output like below.

    enter image description here

    To get the array in required format, use the below expression and assign the value to required variable.

    @json(concat('[',replace(replace(string(variables('temp')), '[', ''),']',''),']'))
    

    enter image description here

    Result:

    enter image description here