cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Post Partisan
Post Partisan

How to get a sub-item of an array?

My array looks like this:

 

[
  {
    "@odata.etag": "W/\"JzEtVGFzayAgQEBAQEBAQEBAQEBAQEBARCc=\"",
    "planId": "johCFoq4nEyhOcbgjLkzN5YAFYWU",
    "bucketId": "uoRC3WMr70WkaUCOGWZ69JYAJEHv",
    "title": "Reception, validation and account of local suppliers invoices",
    "orderHint": "8585975908743310360",
    "assigneePriority": "",
    "percentComplete": 0,
    "createdDateTime": "2020-10-30T00:00:11.1465447Z",
    "dueDateTime": "2020-10-30T07:00:10Z",
    "hasDescription": false,
    "previewType": "automatic",
    "referenceCount": 0,
    "checklistItemCount": 0,
    "activeChecklistItemCount": 0,
    "id": "vd9Lt6swoU6yUsBenCKW9ZYAFmAE",
    "createdBy": {
      "user": {
        "id": "25e6bf64-416e-45c2-8350-9f747027d3c5"
      }
    },
    "appliedCategories": {},
    "assignments": {
      "9c950320-9981-45a3-b31e-2cd31d69d51a": {
        "@odata.type": "#microsoft.graph.plannerAssignment",
        "assignedDateTime": "2020-10-30T00:00:11.1465447Z",
        "orderHint": "8585975909344403874Pg",
        "assignedBy": {
          "user": {
            "displayName": null,
            "id": "25e6bf64-416e-45c2-8350-9f747027d3c5"
          }
        }
      }
    },

 

In order to get the title, the following expression works well:

 

items('Apply_to_each')?['title']

 

But I want the user whose taks was assigned to. So, in the JSON this is under the "assignements" element, as a sub-element of the main object. Thus:

 

items('Apply_to_each')?['assignements']

 

Returns an empty string.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Memorable Member
Memorable Member

This is what you need to do, it gives this result:

AssigneesResult.png

Just a simple array of every assignee. You can see the flow here https://ibb.co/rxR7YcW :

Assignees.png

You need to swap out "somearray" in the foreach for your actual array. Then the rest can remain the same.

 

View solution in original post

17 REPLIES 17
Highlighted
Super User
Super User

Hi @WebPortal 

Try the below options. In your sample JSON, there is no assigned to so I tried to use the assigned by property

1. items('Apply_to_each')?['assignements/assignedby']

2. items('Apply_to_each')?['assignements/assignedby/user/id']

3. items('Apply_to_each')?['assignements']?['assignedby']

4. items('Apply_to_each')?['assignements']?['assignedby']?['user']?['id']

Try it out and let me know

 

Hope it helps, please like it or mark it as a solution if it resolves your clarification or issue
-Sudharsan K...
Highlighted

@sudharsan1985 maybe you misunderstood my question.

 

I'm not looking for the "assigned by", ie, the person who assigned the task.

 

I'm looking for the assignee, ie, the person responsible to perform the task. 

 

So, I think it is this part of the JSON what I need:

 

"assignments": {
      "9c950320-9981-45a3-b31e-2cd31d69d51a": {
Highlighted
Responsive Resident
Responsive Resident

@WebPortal ,

 

if the JSON expression that you provided at the end of your post is what you have in your flow, the reason that you're returning an empty string might be that you misspelled "assignments" by adding an "e" between the "n" and "m" and that i think you need to add an underscore (_) to the front of it like this:

 

items('Apply_to_each')?['_assignments']

JSON can be tricky

I hope this helps.

 

thanks,

Kyle

Highlighted
Post Partisan
Post Partisan

@kdavis91 thanks,

 

That is returning everything under "assignments". 

 

All I need is:

9c950320-9981-45a3-b31e-2cd31d69d51a

(And then, for another post, how to translate that into the user email and name.)

 

Thanks for your help!

Highlighted
Responsive Resident
Responsive Resident

@WebPortal 

 

OK, so it looks like since the assignee value can be it's own array (multiple assignees) you have to loop through all "assignments" even if there's only one.

 

a trick to troubleshooting the JSON is to use Dynamic Content provided by Power Automate and then look at the syntax by hovering your cursor over the Dynamic Content.

 

so basically, within your loop that's iterating through the tasks, you need another apply to each loop to go through the "assignments" pulling out the userId of the assignee(s):

PlannerTaskAssignee.png

 

You can then use the get user data action block, pass in the User ID, and get their email address:

 

GetUserProfile.png

 

Let me know how it goes!

 

Kyle

 

 

 

 

Highlighted

@kdavis91  thank you for helping.

 

I've tried a lot of stuff, but I can't make it. Now, I'm getting the error:

 

The execution of template action 'Apply_to_each_2' failed: the result of the evaluation of 'foreach' expression '@items('Apply_to_each')' is of type 'Object'. The result must be a valid array.

 

WebPortal_0-1604701330985.png

 

Highlighted
Responsive Resident
Responsive Resident

@WebPortal ,

 

It looks like you're going through an extra step of putting the Task details into an array since the task details already return as an array. If your first "Apply to Each" loop iterates through the output of "get tasks", you'll get the same array as the screenshot in your first post above.

 

also, for your second loop, rather than looping through "current item" you should be looping through "assignments", from there you can pull the "assignments user ID" for however you need to use it.

Highlighted
Memorable Member
Memorable Member

@WebPortal this would be my approach. You need to replace variables('someArray') with your actual output (or create a variable called someArray with the content of your output:

GetElement.PNG

 

The second compose gives this output:

composeOutput.PNG

 

Code for the first compose:

 

substring(string(variables('someArray')[0]['assignments']),2)

 

Code for the second compose:

 

substring(outputs('Compose'), 0, indexOf(outputs('Compose'), '"'))

 

It can all be done in a single compose easily. But I thought it would be more readable in this form.

 

Explanation

Your output is an array, but presumably it has only one value. So I accessed the first element of the array and then the assignments property of it. Which gets you down to just this:

 

{
	"9c950320-9981-45a3-b31e-2cd31d69d51a": {
		"assignedDateTime": "2020-10-30T00:00:11.1465447Z",
		"orderHint": "8585975909344403874Pg",
		"assignedBy": {
			"user": {
				"displayName": null,
				"id": "25e6bf64-416e-45c2-8350-9f747027d3c5"
			}
		}
	}
}

 

Then the first compose turns the output into a string, and collects everything except the first two characters. Which leaves you with this:

 

9c950320-9981-45a3-b31e-2cd31d69d51a":{"assignedDateTime":"2020-10-30T00:00:11.1465447Z","orderHint":"8585975909344403874Pg","assignedBy":{"user":{"displayName":null,"id":"25e6bf64-416e-45c2-8350-9f747027d3c5"}}}}

 

The second compose gets a substring of the first compose and stops when it finds the first double quotation mark. Which leaves you with this:

 

9c950320-9981-45a3-b31e-2cd31d69d51a

 

Try it and see how you get on.

Highlighted

@kdavis91thank you for your help.

 

also, for your second loop, rather than looping through "current item" you should be looping through "assignments", from there you can pull the "assignments user ID" for however you need to use it.


That sure makses sense... But WHERE can I find the option to loop through assignments?

 

Captura de ecrã 2020-11-11 102156.jpg

Highlighted

@Paulie78 

 

It's almost working.

 

The array "Tarefas" in my flow contains multiple elements.

 

For every element, the output 2 is always:

 

9c950320-9981-45a3-b31e-2cd31d69d51a

 

So, I guess the 1st compose is not working well.

 

This is how it all looks like now:

 

WebPortal_0-1605098938601.png

 

Highlighted
Memorable Member
Memorable Member

If you give me the entire array (put it on pastebin.com ) I will see if I can extract both values using the same method.

Highlighted

It should only be a matter of replacing [0] wiht [i].

But in Power Automate language, this is probably very complicated.

I'll try to send you the entire array.

Thanks

Highlighted
Memorable Member
Memorable Member

This is what you need to do, it gives this result:

AssigneesResult.png

Just a simple array of every assignee. You can see the flow here https://ibb.co/rxR7YcW :

Assignees.png

You need to swap out "somearray" in the foreach for your actual array. Then the rest can remain the same.

 

View solution in original post

Highlighted
Post Partisan
Post Partisan

It finally worked, thank you!

Highlighted
Memorable Member
Memorable Member

Legend - well done! 👍👍

Highlighted
Post Partisan
Post Partisan

Sure 🙂 still, a long way to go. Now, I need to get the emails of each assignee and group the tasks by each one and send one email per day for each user with a list of due tasks.

I'm sure this will be another 40 hours of work, before it gets automated 🙂

Highlighted
Responsive Resident
Responsive Resident

@WebPortal ,

 

Glad you got it working!

 

Kyle

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Top Solution Authors
Top Kudoed Authors
Users online (8,663)