Home > Back-end >  Filter an array in Azure Logic Apps
Filter an array in Azure Logic Apps

Time:03-16

This seems like it should be straightforward but the fact it isn't suggests I haven't understood something.
I have a simple array variable with content as below. I want to filter it so I only see items where policy is X so that I can get the value for the document element.

I've tried:

  • filter @variables('myArray') where @equals('policy','X')
  • @contains('policy','X')
  • filter @variables('myArray') where @contains(@variables('myArray'),'X'

In each case the array enters the filter array action whole and leaves it completely empty. Any assistance gratefully received.

[

  {
    "document": "A",
    "min": 7500001,
    "policy": "X"
  },

  {
    "document": "B",
    "min": 7500001,
    "policy": "Y"
  },

  {
    "document": "C",
    "min": 7500001,
    "policy": "Z"
  }

]

CodePudding user response:

You can use Parse JSON before Filtering the array. Considering the sample which you have provided we have tested it in our Logic App and this is working. Here is the screenshot of my Logic App for your Reference:

enter image description here

RESULT:

enter image description here

Considering Another Sample of Array

[
  {
    "document": "A",
    "min": 7500001,
    "policy": "X"
  },
  {
    "document": "B",
    "min": 7500001,
    "policy": "Y"
  },
  {
    "document": "C",
    "min": 7500001,
    "policy": "Z"
  },
{
    "document": "D",
    "min": 7500002,
    "policy": "X"
  }
]

RESULT:

enter image description here

Below is the code view of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Filter_array_2": {
                "inputs": {
                    "from": "@body('Parse_JSON')",
                    "where": "@equals(item()['policy'], 'X')"
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Query"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "SampleArray1",
                            "type": "array",
                            "value": [
                                {
                                    "document": "A",
                                    "min": 7500001,
                                    "policy": "X"
                                },
                                {
                                    "document": "B",
                                    "min": 7500001,
                                    "policy": "Y"
                                },
                                {
                                    "document": "C",
                                    "min": 7500001,
                                    "policy": "Z"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@variables('SampleArray1')",
                    "schema": {
                        "items": {
                            "properties": {
                                "document": {
                                    "type": "string"
                                },
                                "min": {
                                    "type": "integer"
                                },
                                "policy": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "document",
                                "min",
                                "policy"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
  • Related