How to find array index based on domain name value using Get Array List Index?

Hi everyone!

I'm trying to find the index of a specific domain in my API response array. Here's what I have:

  1. An API response that returns data in this structure:
{
  "retornoData": [
    {
      "handle": [
        {
          "handler": "reverse_proxy",
          "upstreams": [
            {
              "dial": "127.0.0.1:3000"
            }
          ]
        }
      ],
      "match": [
        {
          "host": [
            "example1.domain.com"
          ]
        }
      ],
      "terminal": true
    },
    {
      "handle": [
        {
          "handler": "reverse_proxy",
          "upstreams": [
            {
              "dial": "127.0.0.1:3000"
            }
          ]
        }
      ],
      "match": [
        {
          "host": [
            "example2.domain.com"
          ]
        }
      ],
      "terminal": true
    },
    {
      "handle": [
        {
          "handler": "reverse_proxy",
          "upstreams": [
            {
              "dial": "127.0.0.1:3000"
            }
          ]
        }
      ],
      "match": [
        {
          "host": [
            "example3.domain.com"
          ]
        }
      ],
      "terminal": true
    }
  ]
}
  1. My Flow setup:
  • API Action that gets this data
  • Create Array List (domainList) with the API response
  • Get Array List Index to find a specific domain's position

I've tried several approaches:

match[0].host[0] == 'example3.domain.com'

.match[0].host[0] == 'example3.domain.com'

host[0] == 'example3.domain.com'

But I keep getting this error:

{
"status": "500",
"message": "arraylist.get: index is required.",
"stack": "Error: arraylist.get: index is required.\n at App.parseRequired (/opt/node_app/lib/core/app.js:759:13)\n at exports.get (/opt/node_app/lib/modules/arraylist.js:41:22)"
}

I need to find the index of a specific domain in this array. For example, if I search for "example2.domain.com", it should return 1 (the second position in the array).

What's the correct syntax to use Get Array List Index in this case?

Thank you for your help!

You can find an index of an array value by using the indexOf, but it doesn't work with array structure like that, it works only with simple arrays like:

["example1.domain.com", "example2.domain.com", "example3.domain.com"]

Maybe you can create an array containing just the domains as in the example above and then run the indexOf step to find the index of the specific domain.

1 Like

Hi @Teodor ,

Thanks for your previous answer! I understand that I need to create a simple array with just the domains first. I tried several approaches, but I couldn't get the correct syntax from Wappler:

  1. Using Set Value:

api_retornoData.map('match[0].host[0]')

  1. Using Set Value:

api_retornoData.map(item => item.match[0].host[0])

But I get parser errors saying that these formatters don't exist.

What is the correct Wappler syntax to transform this complex array:

{
"returnData": [
{
"match": [
{
"host": [
"example1.domain.com"
]
}
]
},
{
"match": [
{
"host": [
"example2.domain.com"
]
}
]
}

Into a simple array?

Thanks!

Here's a hint, it's not just a formatter, you have to play with Repeat steps. You have to iterate that complex object using Repeat step(s) and then you add the domain names to an array

Each property whose value starts with a square bracket is iterable by a Repeat step

The host property has a value that starts with a square bracket, but that doesn't need to be iterated, you can directly access the index [0]. Same for the match property

Edit: In alternative it might be possible to use the flatten formatter, but I'm writing this comment on my phone so I didn't look too much at the JSON

1 Like

When using:

api_retornoData.data.flatten('match')

I get this:

[
{
"host": [
"example1.domain.com"
]
}
],
[
{
"host": [
"example2.domain.com"
]
}
],
[
{
"host": [
"example3.domain.com"
]
}
],
[
{
"host": [
"example4.domain.com"
]
}
]

After that I can't evolve anymore, not even using the repeater to iterate the items, add them to a new array or even count the domain I need.

I tried using other formatter combinations too, but without success so far...

Use one Repeater step and show a screenshot trying to add the host to an array