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

Community site session details

Session Id : sv7u0YcgFJP6E3EcIr7wZf
Power Automate - Building Flows
Unanswered

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

Like (1) ShareShare
ReportReport
Posted on 5 Jul 2024 13:48:09 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

I have the same question (0)
  • maxon Profile Picture
    25 on 24 Jul 2024 at 07:06:16
    How to loop through object variable to get random records and populate them to an array
    Hi,

    Thank you CassioM & Chriddle, its working, I had to only add the step to parse JSON before select nodes. 
     
    Thanks!
    Max
  • CassioM Profile Picture
    13 on 08 Jul 2024 at 12:23:48
    Re: How to loop through object variable to get random records and populate them to an array

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

  • Chriddle Profile Picture
    8,243 Super User 2025 Season 2 on 06 Jul 2024 at 09:53:36
    Re: How to loop through object variable to get random records and populate them to an array

    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 05 Jul 2024 at 16:48:21
    Re: How to loop through object variable to get random records and populate them to an array

    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": ""
     }
    ]
  • David_MA Profile Picture
    12,395 Super User 2025 Season 2 on 05 Jul 2024 at 16:40:52
    Re: How to loop through object variable to get random records and populate them to an array

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Tom Macfarlan – Community Spotlight

We are honored to recognize Tom Macfarlan as our Community Spotlight for October…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 707 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 499 Moderator

#3
chiaraalina Profile Picture

chiaraalina 312

Last 30 days Overall leaderboard
Loading complete