Search code examples
azure-logic-apps

Logic Apps: Filter special characters from array


lets say you have an array called ips:

[
  "[\"185.241.208.232",
  "194.26.192.64",
  "171.25.193.25",
  "\"]"
]

enter image description here

how would I filter out the [" and "] out of the array.

the array that enters the watchlist should look like:

[
  "185.241.208.232",
  "194.26.192.64",
  "171.25.193.25"
]

i need to do this without using a for loop to look at every item for the \ (as the actual list is very long and it'll make the logic app run for some time). i happen to know the \ exists at the start and end of the list.


Solution

  • You don't need to use For each loops to achieve the desired result - this is very inefficient.

    You need to use only 2 actions:

    1. Select - to remove unnecessary characters from each item of your array:

    enter image description here

    Code View:

    {
        "inputs": {
            "from": [
                "[\"185.241.208.232",
                "194.26.192.64",
                "171.25.193.25",
                "\"]"
            ],
            "select": "@replace(replace(replace(item(), '[', ''), ']', ''), '\"', '')"
        }
    }
    
    1. Filter array - to remove empty items from the array:

    enter image description here

    Code View:

    {
        "inputs": {
            "from": "@body('Select')",
            "where": "@greater(length(item()), 0)"
        }
    }