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
A solution without an apply to each loop is always a better solution.
Nicely done!
I also fiddled around with this 😉
Getting the keys is already shown by @CassioM
Compose (Your JSON sanitized)
{
"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"
}
]
I guess this is what you need:
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))
Inputs:
union(body('Select_nodes'), body('Select_nodes'))
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": ""
}
]
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.
Michael E. Gernaey
497
Super User 2025 Season 2
David_MA
436
Super User 2025 Season 2
Riyaz_riz11
244
Super User 2025 Season 2