How to use filter array to filter data from api call

Trying to make a logic app work but just cant seem to finish off the last step - I have had a similar logic app working in the past with these steps but for the life of me i cannot remember the final steps to get it functional.

Would appreciate any help anyone can give. I've included a screenshot + some code below.

Logic app flow Picture

I have redacted some of the code for obvious reasons. Hopefully it is enough for you to get a general feel for what is happening.

Every step works fine apart from the filter array which errors, i've tried a few combinations of the filter array but with no luck.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Filter_array": {
                "inputs": {
                    "from": "@body('Parse_JSON')?['data']",
                    "where": "@contains(item(), body('Select'))"
                },
                "runAfter": {
                    "Select": [
                        "Succeeded"
                    ]
                },
                "type": "Query"
            },
            "For_each": {
                "actions": {
                    "Send_Data": {
                        "inputs": {
                            "body": "@{items('For_each')}",
                            "headers": {
                                "Log-Type": "REDACTED"
                            },
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['azureloganalyticsdatacollector']['connectionId']"
                                }
                            },
                            "method": "post",
                            "path": "/api/logs"
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "foreach": "@body('Filter_array')",
                "runAfter": {
                    "Filter_array": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "HTTP": {
                "inputs": {
                    "headers": {
                        "Authorization": "API-Token REDACTED"
                    },
                    "method": "GET",
                    "queries": {
                        "start_date": "@variables('startDate')"
                    },
                    "uri": "REDACTED"
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Http"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "startDate",
                            "type": "string",
                            "value": "@{addDays(utcnow(), -5, 'yyyy-MM-dd')}"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@body('HTTP')",
                    "schema": {
                        "properties": {
                            "body": {
                                "properties": {
                                    "data": {



                                       REDACTED      



                             
                                    }
                                },
                                "type": "object"
                            },
                            "statusCode": {
                                "type": "integer"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "HTTP": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            },
            "Run_query_and_list_results": {
                "inputs": {
                    "body": "REDACTED",
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azuremonitorlogs']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/queryData",
                    "queries": {
                        "resourcegroups": “REDACTED”,
                        "resourcename": "REDACTED",
                        "resourcetype": "REDACTED",
                        "subscriptions": "REDACTED",
                        "timerange": "Last 7 days"
                    }
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            },
            "Select": {
                "inputs": {
                    "from": "@body('Run_query_and_list_results')?['value']",
                    "select": "@item()?['checkpoint_s']"
                },
                "runAfter": {
                    "Run_query_and_list_results": [
                        "Succeeded"
                    ]
                },
                "type": "Select"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "Recurrence": {
                "recurrence": {
                    "frequency": "Hour",
                    "interval": 1
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
REDACTED
                }
            }
        }
    }
}


Below is a screenshot of what the above code produces in the filter array

Filter Array Error

Breakdown of what happens:

  1. I do an API call for data, i put this through parse JSON. (this part works fine - unless there is something wrong with the parse json definitions but i dont think there is)

  2. I then do a lookup in log analytics and put this into a list using select (this part works fine)

  3. Then i compare these two data sets and filter out the ones that are already present based on a specific field in the API data against the output from the select. (I believe this is the problem area)

  4. I finally put the output from the filter into log analytics (this part works fine)

Edit:

Here is what the data from the Parse JSON roughly looks like (just the layout without any real data)

[
 {
  DATA,
  "checkpoint": "UNIQUECODE",
  DATA
 },
 {
  DATA,
  "checkpoint": "UNIQUECODE",
  DATA
 }
]

Here is the list that come out of the select statement that needs to be compared against to remove already present records:

[
 "UNIQUECODE",
 "UNIQUECODE",
 "UNIQUECODE"
]

1 answer

  • answered 2021-05-04 05:18 Frank Gong

    The provided types 'Object' and 'Array' are incompatible.

    In the expression contains(item(), body('Select')), item() is of type Object, and body('Select') is of type Array. These two items are not allowed to use contains expressions.

    If item() is of type Object, and body('Select') should be of type string.

    For example, this expression is ok:

    contains(item(), body('Select')[0])
    

    Solution:

    You can use Inline Code to filter your array:

    enter image description here

    Code example:

    const arr = ['a', 'd', 'f'];
    const obj = {
       "a": 5,
       "b": 8,
       "c": 4,
       "d": 1,
       "e": 9,
       "f": 2,
       "g": 7
    };
    const filterObject = (obj, arr) => {
       Object.keys(obj).forEach((key) => {
          if(!arr.includes(key)){
             delete obj[key];
          };
       });
    };
    filterObject(obj, arr);
    
    return obj;
    

    Note:

    If you use Inline Code in Azure Logic App You need to add an integration accounts.