Hello everyone!
I need to this below array into one array with project and its relevant hours.
//Current Array I have

Hello everyone!
I need to this below array into one array with project and its relevant hours.
//Current Array I have
Hopefully this is what you're looking for.
For this example, I added an extra user (David Jones) to your initial array.
[
{
"Employee": "Jhon Doe",
"Project": "PS-023WR",
"Hours": 16
},
{
"Employee": "Jhon Doe",
"Project": "KATE-A336",
"Hours": 2
},
{
"Employee": "David Jones",
"Project": "CAR-253A",
"Hours": 25.5
},
{
"Employee": "Jhon Doe",
"Project": "SITE-KJ85",
"Hours": 15
},
{
"Employee": "Jhon Doe",
"Project": "BOAT-253F",
"Hours": 13.5
}
]
See full flow below. I'll go into each of the actions.
Array is a Compose that contains the initial items in your array.
Select extracts out a list of all the Employees. It uses the following expression.
item()?['Employee']
Initialize variable data creates a variable of type Array called data. This will contain all the objects once the flow completes.
Initialize variable employee creates a variable of type Object called employee. This will contain each of the employee data as we build up the data array.
Apply to each (the outer loop) iterates over each of the employees. It uses the following expression as input which provides a unique list of employees.
union(body('Select'), body('Select'))
Set variable employee sets our employee variable to an object containing the current employee. The expression that you can paste directly into the action is below.
{
"Employee": @{item()}
}
Filter array uses the output from Array (Compose) and filters on the current employee. The expression used is:
item()?['Employee']
Apply to each item (the inner loop) uses the output from the Filter array which will iterate over each of the projects for the current employee.
Compose uses the following expression to add a new property to the employee variable. It uses the value of the project as the name of the property, and the value of the hours as the value.
addProperty(variables('employee'), item()?['Project'], item()?['Hours'])
Set variable project sets our employee variable to the output from our Compose.
Append to array variable (after the inner loop) then appends our employee object to the data array.
This will result in the data variable (or array) containing all the employees and their associated project data as shown below:
[
{
"Employee": "Jhon Doe",
"PS-023WR": 16,
"KATE-A336": 2,
"SITE-KJ85": 15,
"BOAT-253F": 13.5
},
{
"Employee": "David Jones",
"CAR-253A": 25.5
}
]
----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.