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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to loop through ob...
Power Automate
Unanswered

How to loop through object variable to get random records and populate them to an array

(1) ShareShare
ReportReport
Posted on by 25

Hi,

I have the following problem with my flow. Namely, I have a variable 'sourceData' of type array based on which I group the data into an object variable 'groupedData' based on one column 'Service'. This works fine, as a result I get a variable containing data in the following format:

 

{
 "body": {
 "name": "groupedData",
 "value": {
 "Digital Private": [
 {
 "@odata.etag": "",
 "ItemInternalId": "c2e3b85b",
 "Number": "31266",
 "Service": "Digital Private",
 "Approval": "2024-05-07T07:57:37.000Z",
 "State": "Closed"
 }
 ],
 "Trading": [
 {
 "@odata.etag": "",
 "ItemInternalId": "9d967542",
 "Number": "29756",
 "Service": "Trading",
 "Approval": "2024-05-16T15:15:17.000Z",
 "State": "Closed",
 },
 {
 "@odata.etag": "",
 "ItemInternalId": "6b1b6998",
 "Number": "31519",
 "Service": "Trading",
 "Approval": "2024-05-06T13:02:21.000Z",
 "State": "Closed"
 }
 ],
 "Digital Guide": [
 {
 "@odata.etag": "",
 "ItemInternalId": "89a40159",
 "Number": "31585",
 "Service": "Digital Guide",
 "Approval set": "2024-05-01T10:26:17.000Z",
 "State": "Closed"
 }
 ]
 }
 ]
 }

In the next step, out of each group (Digital Private, Trading, Digital Guide etc., I need to take one random record and create a 'finalData' variable of type array, which I will use later.

I have no idea how to do that, I were trying with using keys(), but I have an error 
"The variable 'keysArray' of type 'Array' cannot be initialized or updated with value of type 'String'. The variable 'keysArray' only supports values of types 'Array'."

 

I have already lost my ideas.

 

Thank you in advance for help!

Max

Categories:
I have the same question (0)
  • David_MA Profile Picture
    14,418 Super User 2026 Season 1 on at

    I am not the best at this and I always struggle with the expressions, but are you familiar with how to get one of the values out of your array using expressions such as: outputs('Get_items')?['body/value'][2][outputs('Get_response_details')?['body/YourColumnInternalName']]

     

    If so, then you know the [2] would represent the third item in your array. To get a random item use the rand expression and set the minimum value to 0, since 0 represents the first item in the array. For the max value of the of rand, use the length expression to get how many items are in the array and subtract 1 to account for the first item starting at zero. The expression would be something like this: rand(0, length(variables('myArray')) - 1) that you can set in an integer variable.

     

    Then use the variable in place of the 2 in [2] to return a random item from the array.

  • CassioM Profile Picture
    13 on at

    I guess this is what you need:


    CassioM_0-1720197361923.png

    Select Nodes From:

    xpath(
     xml(json(concat('{"Root":', variables('varSourceData')['body/value'], '}'))),
     '/Root/*'
    )

    Select Nodes Map:

    xpath(
     item(),
     'name(/*)'
    )

    (Amazing reference from @Chriddle - Solved: Re: array of keys() and array values() expression ... - Power Platform Community (microsoft.com))

     

    CassioM_1-1720197755800.png

    Inputs:

    union(body('Select_nodes'), body('Select_nodes'))

     

    CassioM_2-1720197829477.png

    node Inputs:

    replace(item(), '_x0020_', ' ')

    rand inputs:

    rand(0, sub(length(variables('varSourceData')['body/value'][outputs('node')]), 1))

    Append to array variable Value:

    variables('varSourceData')['body/value'][outputs('node')][outputs('rand')]

     

    This is the final result:

    [
     {
     "ItemInternalId": "c2e3b85b",
     "Number": "31266",
     "Service": "Digital Private",
     "Approval": "2024-05-07T07:57:37.000Z",
     "State": "Closed",
     "@odata.etag": ""
     },
     {
     "ItemInternalId": "9d967542",
     "Number": "29756",
     "Service": "Trading",
     "Approval": "2024-05-16T15:15:17.000Z",
     "State": "Closed",
     "@odata.etag": ""
     },
     {
     "ItemInternalId": "89a40159",
     "Number": "31585",
     "Service": "Digital Guide",
     "Approval set": "2024-05-01T10:26:17.000Z",
     "State": "Closed",
     "@odata.etag": ""
     }
    ]
  • Chriddle Profile Picture
    8,685 Super User 2026 Season 1 on at

    I also fiddled around with this 😉

    Getting the keys is already shown by @CassioM 

    Chriddle_0-1720260220266.png

     

    Compose (Your JSON sanitized)

    Spoiler (Highlight to read)
    {
     "body": {
     "name": "groupedData",
     "value": {
     "Digital Private": [
     {
     "@@odata.etag": "",
     "ItemInternalId": "c2e3b85b",
     "Number": "31266",
     "Service": "Digital Private",
     "Approval": "2024-05-07T07:57:37.000Z",
     "State": "Closed"
     }
     ],
     "Trading": [
     {
     "@@odata.etag": "",
     "ItemInternalId": "9d967542",
     "Number": "29756",
     "Service": "Trading",
     "Approval": "2024-05-16T15:15:17.000Z",
     "State": "Closed"
     },
     {
     "@@odata.etag": "",
     "ItemInternalId": "6b1b6998",
     "Number": "31519",
     "Service": "Trading",
     "Approval": "2024-05-06T13:02:21.000Z",
     "State": "Closed"
     }
     ],
     "Digital Guide": [
     {
     "@@odata.etag": "",
     "ItemInternalId": "89a40159",
     "Number": "31585",
     "Service": "Digital Guide",
     "Approval set": "2024-05-01T10:26:17.000Z",
     "State": "Closed"
     }
     ]
     }
     }
    }
    { "body": { "name": "groupedData", "value": { "Digital Private": [ { "@@odata.etag": "", "ItemInternalId": "c2e3b85b", "Number": "31266", "Service": "Digital Private", "Approval": "2024-05-07T07:57:37.000Z", "State": "Closed" } ], "Trading": [ { "@@odata.etag": "", "ItemInternalId": "9d967542", "Number": "29756", "Service": "Trading", "Approval": "2024-05-16T15:15:17.000Z", "State": "Closed" }, { "@@odata.etag": "", "ItemInternalId": "6b1b6998", "Number": "31519", "Service": "Trading", "Approval": "2024-05-06T13:02:21.000Z", "State": "Closed" } ], "Digital Guide": [ { "@@odata.etag": "", "ItemInternalId": "89a40159", "Number": "31585", "Service": "Digital Guide", "Approval set": "2024-05-01T10:26:17.000Z", "State": "Closed" } ] } } }

    Select

    From: ["Digital Private", "Trading", "Digital Guide"]

    Map:

     

    outputs('Compose')['body/value'][item()][
    	rand(
    		0,
    		length(
    			outputs('Compose')['body/value'][item()]
    		)
    	)
    ]

     

     

     

    Result:

     

    [
     {
     "@odata.etag": "",
     "ItemInternalId": "c2e3b85b",
     "Number": "31266",
     "Service": "Digital Private",
     "Approval": "2024-05-07T07:57:37.000Z",
     "State": "Closed"
     },
     {
     "@odata.etag": "",
     "ItemInternalId": "6b1b6998",
     "Number": "31519",
     "Service": "Trading",
     "Approval": "2024-05-06T13:02:21.000Z",
     "State": "Closed"
     },
     {
     "@odata.etag": "",
     "ItemInternalId": "89a40159",
     "Number": "31585",
     "Service": "Digital Guide",
     "Approval set": "2024-05-01T10:26:17.000Z",
     "State": "Closed"
     }
    ]

     

     

  • CassioM Profile Picture
    13 on at

    A solution without an apply to each loop is always a better solution.
    Nicely done!

  • maxon Profile Picture
    25 on at
    Hi,

    Thank you CassioM & Chriddle, its working, I had to only add the step to parse JSON before select nodes. 
     
    Thanks!
    Max

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 881

#2
Valantis Profile Picture

Valantis 823

#3
Haque Profile Picture

Haque 485

Last 30 days Overall leaderboard