How to iterate with for in this result set?

Hello
In on API call i m getting this result :

    {
    "catalogs" = []
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaarbtrtpttu4j4ju76a7qtstxgpzvqithwdsoniim7oahmt4slupba"
    "id" = "2020-07-29 18:31:06.183877741 +0000 UTC"
    "state" = "ACTIVE"
    },
    {
    "catalogs" = []
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaaw525xduqwo5psovhbqtby6ggdwpoajut7xeqmsgyoogrjoxfxdma"
    "id" = "2020-07-29 18:31:07.340353027 +0000 UTC"
    "state" = "ACTIVE"
    },
    {
    "catalogs" = []
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaammhfytejwkj7md7lhe2is4yzooh33zdrnyncmoh6ulpse2v2oqaq"
    "id" = "2020-07-29 18:31:07.617347927 +0000 UTC"
    "state" = "ACTIVE"
    },
    {
    "catalogs" = [
    {
    "attached_catalog_private_endpoints" = []
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaavb7wbs7v5naofrzd6qinqvxxuicxmbbiktofwbrquo7afr3kqqyq"
    "defined_tags" = {
    "Mandatory_Tag.CreateDate" = "2020-02-25T07:50:54.848Z"
    "Mandatory_Tag.CreatedBy" = "ocid1.saml2idp.oc1..aaaaaaaaofk4hczswo3eewraxwcmldcilwhrkvv6ajgn4lgk5wqfb7wafcsa/said.nechab@oracle.com"
    }
    "display_name" = "SWATDataCatalog"
    "freeform_tags" = {}
    "id" = "ocid1.datacatalog.oc1.eu-frankfurt-1.aaaaaaaaj7i4mfnpyvcsggonyojm6dpd6tgd75glukannjgmhxy2genffawq"
    "lifecycle_details" = "Catalog Created"
    "number_of_objects" = 0
    "service_api_url" = ""
    "service_console_url" = ""
    "state" = "ACTIVE"
    "time_created" = "2020-02-25 07:50:56.5 +0000 UTC"
    "time_updated" = "2020-02-25 07:50:56.5 +0000 UTC"
    },
    ]
    "compartment_id" = "ocid1.compartment.oc1..aaaaaaaavb7wbs7v5naofrzd6qinqvxxuicxmbbiktofwbrquo7afr3kqqyq"
    "id" = "2020-07-29 18:31:06.479666851 +0000 UTC"
    "state" = "ACTIVE"
    }

Eveything is contained into a local variabe :

locals {
res_api = data.oci_apigateway_gateways.gateways[*].gateway_collection[*]
}

My question is how can i iterate the above local variable and skip all entries with the "catalogs" = [] and get only the entrie where the catalog != []

Many thanks for any hint

You can do this with a for expression and the optional if clause. Here’s a simple example:

locals {
  results = [
    {
      "catalogs": [],
      "id": 5,
    },
    {
      "catalogs": ["foo", "bar"],
      "id": 9,
    },
    {
      "catalogs": ["baz"],
      "id": 17,
    },
  ]

  catalog_ids = [ for r in local.results: r if length(r.catalogs) > 0 ]
}

output "catalog_ids" {
  value = local.catalog_ids
}
$ terraform apply -auto-approve

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

catalog_ids = [
  {
    "catalogs" = [
      "foo",
      "bar",
    ]
    "id" = 9
  },
  {
    "catalogs" = [
      "baz",
    ]
    "id" = 17
  },
]

Hope this helps!

(As a side note, please try to use preformatted text when pasting any configuration or data into forum posts—it makes it much easier to read and work with when answering questions. Thanks! :slight_smile: )

Thanks a lot , sure next time i will format the code :+1: