I have two arrays that have respectively contain keys and values:
array 1 ["key1", "key2", "key3"]
and array 2 ["value1", "value2", "value3"]
With ADF data flow expression, I can construct a key-value map with these two arrays using keyValues
function:
keyValues(["key1", "key2", "key3"],["value1", "value2", "value3"])
And this will return a result like this:
["key1" -> "value1", "key2" -> "value2", "key3" -> "value3"]
I want to construct the same key-value map within a pipeline, not data flow, but the keyValues
function is not available in pipeline expression. How can I construct a key-value map with pipeline expression on Azure Data Factory / Synapse Studio?
CodePudding user response:
There is no direct way to create a key value pair using pipeline expression builder as in dataflows. The following is one to build the required key value pairs in ADF pipelines:
- I have taken 2 parameters with the following values:
- Let's say
req
is the variable in which we would like to store our final key value pair. I have initialized it with the value{}
- In
for each
, I have used items value as@range(0,length(pipeline().parameters.keys))
to generate index.
- Now, I have taken a variable called
temp
to apply union on the current value ofreq
and the dynamically built key-value pair for current iteration.
@string(union(json(variables('req')),json(concat('{"',pipeline().parameters.keys[item()],'":"',pipeline().parameters.values[item()],'"}'))))
- Finally, I am updating the value of
req
variable for each iteration with the currenttemp
value i.e.,@variables('temp')
:
- After running the pipeline, it would generate the following output in req variable:
NOTE: Object type variables are not supported in ADF pipelines. Whenever you want to use this object (stored as string), you can convert it to an object type using @json()
function