cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Ajwou
Helper II
Helper II

Length of an array within JSON body/array

Hello, 

 

I have an array that is coming from Parse JSON function. Within that parsed main body/array is another array called departments_ordered. Please would there be an easy way to obtain the number of objects/items within that array? See my current flow below

Ajwou_0-1631103022562.png

 

This actually links to a more general question - how can I "access" the individual items within an array- when using Expression?

 

Thank you, 

 

Ajwou

1 ACCEPTED SOLUTION

Accepted Solutions
Paulie78
Super User
Super User

You probably want:

length(items('Apply_to_each')?['department_ordered'])

If you stick that in a compose action within the loop, then you will be able to verify the output.

 

View solution in original post

12 REPLIES 12
Paulie78
Super User
Super User

You probably want:

length(items('Apply_to_each')?['department_ordered'])

If you stick that in a compose action within the loop, then you will be able to verify the output.

 

View solution in original post

Pstork1
Dual Super User III
Dual Super User III

@Paulie78 's suggestion will work inside the loop.  If you want to do it outside the loop based off the parse json it will be somethng like this:

length(body('Parse_JSON')?['body/value']?['department_ordered'])

The actual code will depend on the name of the root object and the path to the deparment_ordered array.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Ajwou
Helper II
Helper II

Hi there, 

 

Thank you for this. I tried including it in the Compose action (as expression), however it won't let me save the flow - looks like it doesn't like the source?

Ajwou_0-1631106239616.png

 

That is because in your original screenshot you were in an apply/each loop, so I wrote the expression for that situation.

Apologies for that. I should have included the full flow there from the start. 

 

I have used the expression within loop for the condition field and got the following error

Ajwou_0-1631107833887.png

Essentially, I have list of projects where some of them will have multiple departments and I am trying to send projects with multiple departments to one list and projects with only one department to another. 

@Pstork1 - I tried the expression outside the loop, within compose action, but the flow doesn't like it. 

Ajwou_0-1631111173530.png

The JSON schema used for parsing the data looks similar to the below:

Ajwou_1-1631111510651.png

Sorry if some things are not obvious to me. I currently started looking into JSON/data and all is completely new. 

Pstork1
Dual Super User III
Dual Super User III

That would be because your root of the schema is an array, not an object.  You'll need to specify which array item you want to get the length of departments for.  You can do that by putting the compose inside an apply to each or by specifying the specific index that you want to check.  Something like this

length(body('Parse_JSON')?['body/value'][0]?['department_ordered'])

Which should show you the number of departments in the first item of the array.  This is dependent on what the first item in the array is actually called in the parse JSON.  It may not be Body/value.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

@Pstork1 - thank you for helping and patience. The parse JSON output starts with {"body":[{ and then continues in the same way as the JSON schema. The 'departments_ordered' is within the main (body) array (first level) and I am trying to get the number of items within that 'departments_ordered'. I tried putting the 

length(body('Parse_JSON')?['body/value'][0]?['department_ordered'])

Ajwou_1-1631178095341.png

 

within the loop but that doesn't work. I always get the following error:

Ajwou_2-1631178229755.png

Even when I tried with defining the index

Ajwou_3-1631178367800.png

I get similar error

Ajwou_4-1631178570081.png

 

 

 

 

 

 

Pstork1
Dual Super User III
Dual Super User III

Try this.  Add a compose statement inside the Apply to each loop based on the body just before the condition that checks the length.  You'll actually need to add it after the condition and then drag the condition below it.  You can't add a new action to the top of a loop or drag one there.  In that condition add the dynamic content for current item.  Then send me a screenshot of the tooltip you get when you hover over that entry in the compose.  From that I'll be able to tell you what the exact length formula should look like.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Thank you for the advise. I added one more condition and all went under Apply to each, then I check the tooltip as you suggested on Apply to each - department_ordered and used it as the condition - see below:

Ajwou_0-1631200319512.png

It seems to be delivering the required output now, but thought I would check with you if the condition makes sense. 

Also wondering how would the expression look if I would want the length right afterer parsing (outside the Apply to each. This is the tooltip on the Body

Ajwou_1-1631200579737.png

 

 

Pstork1
Dual Super User III
Dual Super User III

For inside the apply to each loop the condition with the length now looks like the right syntax.  Glad its working

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Ajwou
Helper II
Helper II

@Paulie78 @Pstork1 Thank you for your help. The first suggestion from Paulie78 was correct, just needed to be placed in the right location, that took me some time to realise.  

 

One more thing I was wondering - would there be a possibility to exclude certain department name in the syntax: 

Ajwou_0-1631256844630.png

 

I would like to exclude "Internal" from the count - in this case ending up with length 1 instead of 2.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Kudoed Authors
Users online (2,221)