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

Filter array based on nested items - too complicated?

Hi there,

I'm trying to filter a JSON array based on items that are nested in the structure. 
Wondering if there is a way that requires less steps than what I've come up with:
image.png


So the "ForEach" steps through every customer in my array and filters the Value-field within the ProductInterests (Choice-field from a SP list). Then a compose checks if the field is empty. If not empty, then append to variable. 
Wondering if it is possible to skip the condition check and use an if-statement instead (but couldn't find a formula that appends to the array-variable).
Also wondering if there is a way to do this without an ForEach-Loop at all, just with code.

This is the JSON:

{
  "body": {
    "value": [
      {
        "ID": 1,
        "Name": "Good Deals",
        "Adress": "Some Street\nSome City",
        "Active": true,
        "ContactPersons": [
          {
            "DisplayName": "Peter",
            "Email": "Peter@GoodDeals.com"
          },
          {
            "DisplayName": "Nina",
            "Email": "Nina@GoodDeals.com"
          }
        ],
        "ProductInterests": [
          {
            "Id": 1,
            "Value": "Bikes"
          },
          {
            "Id": 2,
            "Value": "Boats"
          }
        ],
        "ProductInterests#Id": [1, 2],
        "CompanySize": {
          "Id": 1,
          "Value": "51-100"
        },
        "CompanySize#Id": 1
      },
      {
        "ID": 2,

        "Name": "Fun Factory",
        "Adress": "Some street\nSome city",
        "Active": false,
        "ContactPersons": [
          {
            "DisplayName": "Juan",
            "Email": "Juan@FunFactory.com"
          }
        ],

        "ProductInterests": [
          {
            "Id": 0,
            "Value": "Cars"
          },
          {
            "Id": 1,
            "Value": "Bikes"
          }
        ],

        "ProductInterests#Id": [0, 1],

        "CompanySize": {
          "Id": 0,
          "Value": "1-50"
        },
        "CompanySize#Id": 0
      }
    ]
  }
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
v-litu-msft
Community Support
Community Support

Hi @Imke,

 

You could approach that feature like the below screenshot:

1. Put the array into the input of Filter array action;

2. convert the ProductInterests property to a string, then use Contains condition to filter out item contains "Boats":

@contains(string(item()?['ProductInterests']), 'Boats')

Screenshot 2021-03-24 135458.jpg

 

Then the output of filter array will contains items which ProductInterests property contains Boats.

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-litu-msft
Community Support
Community Support

Hi @Imke,

 

You could approach that feature like the below screenshot:

1. Put the array into the input of Filter array action;

2. convert the ProductInterests property to a string, then use Contains condition to filter out item contains "Boats":

@contains(string(item()?['ProductInterests']), 'Boats')

Screenshot 2021-03-24 135458.jpg

 

Then the output of filter array will contains items which ProductInterests property contains Boats.

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Imke
Resolver I
Resolver I

Thank you @v-litu-msft ,
that will do the job most of the times. 
But it is not limited to the Value-field only and also includes the field names themselves.
So keeping this thread open a bit just to see if there is a solution that limits the test to the content of the Value field.

v-litu-msft
Community Support
Community Support

Hi @Imke,

 

Ok, you could use the join() function instead of string() function, it will remove the field name:Screenshot 2021-03-24 173959.jpg

 

Screenshot 2021-03-24 174012.jpg

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Imke
Resolver I
Resolver I

Thank you, but I don't understand how to apply this.

What is the input (compose) here?
Are you within a for each-loop?
How can I select value and then still be able to address item()?['ProductInterests']?
The value field I'm interested in, is inside the ProductInterests, so no way for me to fetch that before.
 

v-litu-msft
Community Support
Community Support

Hi @Imke,

 

1. The input (compose) here is the original JSON you provided.

2. It's not within a for each loop, it just did a tiny change based on my previous reply.

3. You mean like this?

Screenshot 2021-03-25 162403.jpg

 

Best Regards,
Community Support Team _ Lin Tu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (2,828)