web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Get data from one arra...
Power Automate
Answered

Get data from one array and add the value to another array

(1) ShareShare
ReportReport
Posted on by 51
Hi Experts
 
I have two array variables with the following content:
 
queueItems array:
 
{
  "name": "queueItems",
  "value": [
    {
      "ExecutorJobKey": "123",
      "Reference": "Ref123",
      "StartProcessing": "2024-10-06T02:34:55.733Z",
      "EndProcessing": "2024-10-06T02:35:14.873Z",
      "Id": 1,
      "ProcessId": null
    },
    {
      "ExecutorJobKey": "456",
      "Reference": "Ref456",
      "StartProcessing": "2024-10-05T20:05:33.667Z",
      "EndProcessing": "2024-10-05T20:05:54.557Z",
      "Id": 2,
      "ProcessId": null
    }
  ]
}
 
jobs array:
 
{
  "name": "jobs",
  "value": [
    {
      "Key": "123",
      "Release": {
        "ProcessKey": "PK123"
      }
    },
    {
      "Key": "456",
      "Release": {
        "ProcessKey": "PK123"
      }
    },
    {
      "Key": "789",
      "Release": {
        "ProcessKey": "PK456"
      }
    },
    {
      "Key": "987",
      "Release": {
        "ProcessKey": "PK789"
      }
    }
  ]
}
 
I need to populate the ProcessId in the queueItems array with the ProcessKey from the jobs array. I can use ExecutorJobKey from the queueItems array to lookup the ProcessKey in the jobs array on the Key.
 
I can do this by using a For Each loop but the amount of transactions will be significant slowing down the flow a lot. Can it somehow be achieved by using the Select activity and then map the ProcessKey?
Categories:
I have the same question (0)
  • Verified answer
    jacchr1 Profile Picture
    51 on at
    So, I found a video on YouTube leading me in some direction to make it work. Not sure if it is the most optimal solution but this is what I did:
     
    I have used a Select activity on the Jobs array variable and under the Map I have switched to Text mode and added the following expression:
     
    concat('"', item()?['Key'], '":', item())
     
     
    This outputs:
     
    [
      "\"123\":{\"Key\":\"123\",\"Release\":{\"ProcessKey\":\"PK123\"}}",
      "\"456\":{\"Key\":\"456\",\"Release\":{\"ProcessKey\":\"PK456\"}}",
      "\"789\":{\"Key\":\"789\",\"Release\":{\"ProcessKey\":\"PK789\"}}",
      "\"987\":{\"Key\":\"987\",\"Release\":{\"ProcessKey\":\"PK987\"}}"
    ]
     
    Then using a Compose action I convert the output from the previous action to a JSON object:
     
    json(concat('{', join(body('Select_[jobs]'), ','), '}'))
     
     
    This returns:
     
    {
        "123": {
            "Key": "123",
            "Release": {
                "ProcessKey": "PK123"
            }
        },
        "456": {
            "Key": "456",
            "Release": {
                "ProcessKey": "PK456"
            }
        },
        "789": {
            "Key": "789",
            "Release": {
                "ProcessKey": "PK789"
            }
        },
        "987": {
            "Key": "987",
            "Release": {
                "ProcessKey": "PK987"
            }
        }
    }

     
    Finally, I am using another Select activity but this time on the Queue Items array where I use the normal mapping function to select the items from the queue items array that I need. Then to map the ExecutorJobKey in the queue items array with the Key in the jobs array I have declared a key name (in my case ProcessId) and in the value field I have added the following expression:
     
    outputs('Compose_[jobsJSON]')?[item()?['ExecutorJobKey']]?['Release']?['ProcessKey']
     
     
    The ProcessKey from the jobs array will now be added to the output of the Select activity and seems to be working faster than using a For Each loop.
  • Suggested answer
    abm abm Profile Picture
    32,865 Most Valuable Professional on at
     Hi
     
    If you are looking to match the ExecutorJobKey with Key from other array you can use xPath expression to achieve this. Here are the steps you need to follow:
     
     
     
    Next I used a Parse JSON step
     
     
     
    Below the schema I used above for the Parse JSON step
     
    {
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            },
            "value": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "Key": {
                            "type": "string"
                        },
                        "Release": {
                            "type": "object",
                            "properties": {
                                "ProcessKey": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "required": [
                        "Key",
                        "Release"
                    ]
                }
            }
        }
    }
     
     
    Next use the select action step to map the values and assign the output of the select under an array variable called Array2.
     
    Here for the matching simplicity I have use the ExecutorJobKey for the Key under the Select. So that later when we use the xPath expression it will be much easier.
     
     
     
     
     
    Next I declared a compose 2 value with the below format
     
     
     
    Above schema for the compose  2 is as follows:
     
    {
      "Root": {
        "data": @{variables('Array2')}
      }
    }
     
    Next I have declared a compose and copy the master data array
     
     
     
    Next I have used a parse json step
     
     
     
    The schema is as follows:
     
    {
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            },
            "value": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "ExecutorJobKey": {
                            "type": "string"
                        },
                        "Reference": {
                            "type": "string"
                        },
                        "StartProcessing": {
                            "type": "string"
                        },
                        "EndProcessing": {
                            "type": "string"
                        },
                        "Id": {
                            "type": "integer"
                        },
                        "ProcessId": {}
                    },
                    "required": [
                        "ExecutorJobKey",
                        "Reference",
                        "StartProcessing",
                        "EndProcessing",
                        "Id",
                        "ProcessId"
                    ]
                }
            }
        }
    }
     
    Next initialise another array variable called Array1 and map the Value from the above Parse JSON step. 
     
     
     
    Final step is
     
     
     
    To get the matching values for the ProcessId we need to use the below expression
     
    if(
        equals(
            length(
                xpath(
                    xml(outputs('Compose_2')),
                    concat('//data[ExecutorJobKey[text()="', item()?['ExecutorJobKey'], '"]]/ProcessId/text()')
                )
            ),
            0
        ),
        '',
        xpath(
            xml(outputs('Compose_2')),
            concat('//data[ExecutorJobKey[text()="', item()?['ExecutorJobKey'], '"]]/ProcessId/text()')
        )?[0]
    )
     
     
    This is my test result:
     
     
     
     
    For simplicity I have used few extra steps above such as compose and initialise variables. You can normalise these steps if you want.
     
    Let me know how it goes.
     
    Thanks
     
     
     
     
     
  • abm abm Profile Picture
    32,865 Most Valuable Professional on at
    Hi jacchr1
     
    The verified answer won't work if you have data in different order. If you looking to verify against ExecutorJobKey and Key value you need to use xpath method. 
     
    Thanks
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard