Skip to main content

Notifications

Power Automate - Building Flows
Answered

Length of an array within JSON body/array

(0) ShareShare
ReportReport
Posted on by

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

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    @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.

  • Pstork1 Profile Picture
    Pstork1 64,703 on at
    Re: Length of an array within JSON body/array

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

     

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    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 Profile Picture
    Pstork1 64,703 on at
    Re: Length of an array within JSON body/array

    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.

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    @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 Profile Picture
    Pstork1 64,703 on at
    Re: Length of an array within JSON body/array

    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.

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    @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. 

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    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. 

  • Paulie78 Profile Picture
    Paulie78 8,407 on at
    Re: Length of an array within JSON body/array

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

  • Ajwou Profile Picture
    Ajwou on at
    Re: Length of an array within JSON body/array

    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

     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,343

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard