cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
PabloRoldan
Resolver I
Resolver I

How to validate if a item exists in array of objects?

Hi, there is a connector to Blob storage that return data like these:

 

 

[
  {
    "Id": "JTJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmY=",
    "Name": "folder1/",
    "DisplayName": "folder1/"
  },
 {
    "Id": "JSJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmS=",
    "Name": "folder2/",
    "DisplayName": "folder2/"
  }
]

 

 

and i'd like to validate if  the result contains  a element where Name == 'folder3/'

thanks

1 ACCEPTED SOLUTION

Accepted Solutions

@PabloRoldan 

 

You can use an Apply to Each to iterate over the Array, use a Condition to verify if the Name field is 'folder3', and if it is, to set a variable to true in that event.

 
 
The other blocks look like this:
 
i-8121-01.png

 

i-8121-3.png

 

Where it says "Body" above for the Apply to Each, you place whatever is the correct Dynamic Content for your Flow whenever it outputs something similar to what you originally posted. 

 

To test it with a hard coded content, you can do as follows:

 

For example, it also works, if we just use a Compose block like this, and just place something similar to what you posted in it, to simulate what is expected there:

 

i-8121-5.png

and then just place the 'Outputs' of 'Compose 2' above, instead of where it says 'Body' in the above screenshot.

 

(notice there is a 3rd item added, it has a name folder3/ -  that is intentional to test if it really works - it was also tested with and without the 3rd element, and found to work correctly in both cases).

 

 

The test run should give the following result in the 3rd iteration:

 

 

i-8121-4-tst.png

 

Notice that in this test, I did change your test input data to include a 3rd element with the name folder3/ to see if the Flow would recognize it, and it does indeed recognize it. Without the 3rd test element, it should leave the variable to false, to signal that no such element was found with a Name key containing a value of 'folder3/'

 

 

The test data used in this case,, was as follows:

 

[
{
"Id": "JTJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmY=",
"Name": "folder1/",
"DisplayName": "folder1/"
},
{
"Id": "JSJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmS=",
"Name": "folder2/",
"DisplayName": "folder2/"
},
{
"Id": "JSJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmQ=",
"Name": "folder3/",
"DisplayName": "folder3Dis/"
}
]

 

It was also tested without the 3rd element - in which case, it iterates only over the first two elements, and, not finding folder3/ in the Name, will intentionally fail to set the variable to true in that case, meaning it was working correctly.

 

Check if the above helps.

 

View solution in original post

7 REPLIES 7

@PabloRoldan 

 

You can use an Apply to Each to iterate over the Array, use a Condition to verify if the Name field is 'folder3', and if it is, to set a variable to true in that event.

 
 
The other blocks look like this:
 
i-8121-01.png

 

i-8121-3.png

 

Where it says "Body" above for the Apply to Each, you place whatever is the correct Dynamic Content for your Flow whenever it outputs something similar to what you originally posted. 

 

To test it with a hard coded content, you can do as follows:

 

For example, it also works, if we just use a Compose block like this, and just place something similar to what you posted in it, to simulate what is expected there:

 

i-8121-5.png

and then just place the 'Outputs' of 'Compose 2' above, instead of where it says 'Body' in the above screenshot.

 

(notice there is a 3rd item added, it has a name folder3/ -  that is intentional to test if it really works - it was also tested with and without the 3rd element, and found to work correctly in both cases).

 

 

The test run should give the following result in the 3rd iteration:

 

 

i-8121-4-tst.png

 

Notice that in this test, I did change your test input data to include a 3rd element with the name folder3/ to see if the Flow would recognize it, and it does indeed recognize it. Without the 3rd test element, it should leave the variable to false, to signal that no such element was found with a Name key containing a value of 'folder3/'

 

 

The test data used in this case,, was as follows:

 

[
{
"Id": "JTJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmY=",
"Name": "folder1/",
"DisplayName": "folder1/"
},
{
"Id": "JSJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmS=",
"Name": "folder2/",
"DisplayName": "folder2/"
},
{
"Id": "JSJmZG9jdW1lbnRsaWJyYXJ5JTJmQ2FydGErZGUrUmVudGElMmYyMDIxMDclMmQ=",
"Name": "folder3/",
"DisplayName": "folder3Dis/"
}
]

 

It was also tested without the 3rd element - in which case, it iterates only over the first two elements, and, not finding folder3/ in the Name, will intentionally fail to set the variable to true in that case, meaning it was working correctly.

 

Check if the above helps.

 

PabloRoldan
Resolver I
Resolver I

@poweractivatethat was an excellent and detailed answer, thank you.

marpio
Advocate I
Advocate I

I think I have a better solution. Apply to each is painfully slow.
Here what I have done

 

1. get items form the SP list

marpio_0-1652105797105.png

 

2. create an html table (limiting to the column which contains the values I am going to search)

marpio_1-1652105820624.png

 

3, searching my string within the output of the HTML table creation (in my case condition was enough)

marpio_2-1652105887378.png

 

I did not count, but apparently it is 100 times faster than apply to each

 

Marco

vivekpande18
Super User
Super User

Hi @PabloRoldan @marpio @poweractivate ,

 

Appreciate your efforts!!

 

I also have one solution which requires just 2 actions to determine, whether the item present in JSON or not.

 

1- Include FILTER action and provide JSON as input

vivekpande18_1-1652107147358.png

item()?['Name'] is equal to folder3/

 

2- add a variable to check, whether the item is present or not.

vivekpande18_2-1652107296401.png

 

Value : greater(length(body('Filter_array')),0)
this will provide output as either true/false based on item presence.
 
Thanks
Vivek
 
If this helps your query, accept it as solution & provide thumbs up..

Hi @marpio FYI this only works for up to 100 entries in order of ID of a list. Let me know if you have a workaround to pull a longer list of data into the HTML Table...

Thanks for this great solution.

Hi @vivekpande18 ,

this may be a newbie question but what does JSON contain?

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (1,963)