Alternative to keyValues Function for Pipeline Expressions in ADF

Glasier 380 Reputation points
2024-07-05T13:03:46.0033333+00:00

I'm working with Azure Data Factory (ADF) and I need to create a key-value map within a pipeline expression. While the keyValues function works perfectly in Data Flows, it's not available in pipeline expressions.

My scenario involves two separate arrays, one containing keys and another containing corresponding values. I want to combine them into a key-value map format like this:

["key1" -> "value1", "key2" -> "value2", "key3" -> "value3"]

Is there a way to achieve this key-value map creation using pipeline expressions in ADF or Synapse Studio?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
10,023 questions
0 comments No comments
{count} votes

Accepted answer
  1. Smaran Thoomu 12,100 Reputation points Microsoft Vendor
    2024-07-05T16:47:29.7433333+00:00

    Hi @Glasier

    Thanks for the question and using MS Q&A platform.

    ASAIK there is no direct way to create key-value pairs using the pipeline expression builder in ADF, like you can in dataflows. Here is a method to build the required key-value pairs in ADF pipelines:

    I have used 2 parameters with these values:

    enter image description here

    Let's say req is the variable where we want to store our final key-value pairs. I have initialized it with an empty object {}.

    enter image description here

    In the 'ForEach' activity, I set the items value to @range(0, length(pipeline().parameters.keys)) to generate the index.

    enter image description here

    Now, I have taken a variable called temp to apply union on the current value of req 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()],'"}'))))
    
    

    enter image description here

    Finally, I update the req variable in each iteration with the current temp value, i.e., @variables('temp').

    enter image description here

    After running the pipeline, it will generate the following output in the req variable:

    enter image description here

    ADF pipelines do not support variables of the object type. If you need to use an object stored as a string, you can convert it to an object using the @json() function.

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful