Performing certain word to uppercase specifically inside the Curley braces in string using terraform

How to perform certain string to uppercase for below scenario,

anything which is inside the Curley braces like surveyId for any complex string

From

/v1/Surveys/{surveyId}
/v1/Surveys/{surveyId}/summary

to

/v1/Surveys/{SURVEYID}
/v1/Surveys/{SURVEYID}/summary

Hi @prasannap,

Sometimes the ‘old’ tools are useful to achieve things like this. The ‘old’ tool I am referring to here is regex!

This regex uses a regex concept of capture groups.
(^.*)({[^}]+})(.*$)
and extracts 3 strings:

  1. (^.*) Everything from the start of the string to the first { (Strictly speaking, up to the next capture group)
  2. ({[^}]+}) Everything between the first pair of curly braces, including the braces.
  3. (.*$) Everything after the first ‘}’ to the end of the string (Strictly speaking everything from the end of the previous capture group to the end of the string)

The Terraform function regexall then returns this as a list of strings.
Using a for expression you can then uppercase the list item that starts and ends with curly braces, and then use the join function to concatenate them back to the original string (with the upper case part).

Note this assumes there is only one instance of something in curly braces:

Sample Code

locals {
  original = "/v1/Surveys/{surveyId}/summary/"

    regex_expression = "(^.*)({[^}]+})(.*$)"

    # uppercase the text within {}
    matches = flatten(regexall(local.regex_expression, local.original))

    # converting any match starting with { and ending with } to uppercase
    matches_upper = [for m in local.matches : startswith(m,"{") && endswith(m,"}")? upper(m) : m ]

    # joining the matches_upper list to get the final result
    result = join("", local.matches_upper)

    # all above stemps in one expression
    all_in_one = join("",[for m in flatten(regexall(local.regex_expression, local.original)) : startswith(m,"{") && endswith(m,"}")? upper(m) : m ])

}

output "matches" {
  value = local.matches
}

output "result" {
  value = local.result
  
}

output "all_in_one" {
  value = local.all_in_one
  
}

Sample Output

Outputs:

all_in_one = "/v1/Surveys/{SURVEYID}/summary"
matches = [
  "/v1/Surveys/",
  "{surveyId}",
  "/summary",
]
result = "/v1/Surveys/{SURVEYID}/summary"

Hope that helps

Happy Terraforming

Thanks Sir @ExtelligenceIT. Still its not working as expected for below json and for some of other json I have

{
  "openapi": "3.0.1",
  "info": {
    "title": "Api",
    "version": "1.0.25"
  },
  "servers": [
    {
      "url": "https://collections"
    }
  ],
  "paths": {
    "/v1/Collections": {
      "post": {
        "tags": [
          "Collections"
        ],
        "summary": "Creates a new collection.",
        "description": "Sample request\r\n\r\n    POST /v1/\r\n    {\r\n        \"id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",\r\n        \"name\": \"Test Collection 43\",\r\n        \"description\": \"Some test collection\",\r\n        \"visibility\": \"Private\",\r\n        \"createdOn\": \"2021-10-20T15:10:46.701Z\",\r\n        \"modifiedOn\": \"2021-10-20T15:10:46.701Z\",\r\n        \"cricutUserId\": 10009061,\r\n        \"profileId\": \"60d0a756f5859d5b581c5237\"\r\n    }",
        "operationId": "CreateCollection",
        "requestBody": {
          "description": "The collection information",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CollectionViewModel"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request. Collection information is missing.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "402": {
            "description": "Maximum free collections reached.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden. Can't create a collection for another user.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Collections/{collectionId}": {
      "get": {
        "tags": [
          "Collections"
        ],
        "summary": "Retrieves the collection with the given collection id.",
        "description": "Sample request\r\n\r\n    GET /v1/e0585d30-31b5-11ec-8d3d-0242ac130003?size=100&token=10",
        "operationId": "GetCollectionById",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "The unique identifier for the collection",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "size",
            "in": "query",
            "description": "Specifies the number of items to return with the collection",
            "required": true,
            "schema": {
              "maximum": 200,
              "minimum": 1,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "token",
            "in": "query",
            "description": "Specifies the current pagination token. This can be ignored on the initial page request.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "itemTypes",
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/ItemType"
              }
            }
          },
          {
            "name": "sortOrder",
            "in": "query",
            "schema": {
              "$ref": "#/components/schemas/SortOrder"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionWithItemsViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "Collections"
        ],
        "summary": "Updates an existing collection.\r\nFields that can be updated include Name, Description, and Visibility",
        "description": "Sample request\r\n\r\n    PUT /v1/3fa85f64-5717-4562-b3fc-2c963f66afa6\r\n    {\r\n        \"id\": \"3fa85f64-5717-4562-b3fc-2c963f66afa6\",\r\n        \"name\": \"Test Collection 45\",\r\n        \"description\": \"Some test collection\",\r\n        \"visibility\": \"Private\",\r\n        \"createdOn\": \"2021-10-20T15:10:46.701Z\",\r\n        \"modifiedOn\": \"2021-10-20T15:10:46.701Z\",\r\n        \"cricutUserId\": 10009061,\r\n        \"profileId\": \"60d0a756f5859d5b581c5237\"\r\n    }",
        "operationId": "SaveCollection",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "The id of the collection to update.",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "description": "The collection information.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CollectionViewModel"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden. Can't update a collection for another user.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "Collections"
        ],
        "summary": "Deletes a collection.",
        "description": "Sample Request:\r\n            \r\n\tDELETE /v1/3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "operationId": "DeleteCollection",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "The id of the collection to remove.",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Success."
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden. Can't delete a collection for another user.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Collections/{collectionId}/counts": {
      "get": {
        "tags": [
          "Collections"
        ],
        "summary": "Retrieves the items counts for the collection with the given collection id.",
        "description": "Sample request\r\n\r\n    GET /v1/e0585d30-31b5-11ec-8d3d-0242ac130003/counts",
        "operationId": "GetCollectionCounts",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "Id of the desired collection.",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionWithItemsViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Collections/{collectionId}/items": {
      "put": {
        "tags": [
          "Collections"
        ],
        "summary": "Adds an item to the given collection.",
        "description": "Sample request\r\n\r\n    PUT /v1/3fa85f64-5717-4562-b3fc-2c963f66afa6/items\r\n    {\r\n        \"itemType\": \"Project\",\r\n        \"itemId\": \"3fa85f64\"\r\n    }",
        "operationId": "AddCollectionItem",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "Id of the collection into which the item should be added.",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "description": "The collection item information.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CollectionItemRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success."
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden. Can't update a collection for another user.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "406": {
            "description": "CricutValidationException occurred, such as the collection was already at max items.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Collections/{collectionId}/items/{itemType}/{itemId}": {
      "delete": {
        "tags": [
          "Collections"
        ],
        "summary": "Removes an item from the given collection.",
        "description": "Sample request\r\n\r\n    DELETE /v1/3fa85f64-5717-4562-b3fc-2c963f66afa6/items/Project/3fa85f64",
        "operationId": "RemoveCollectionItem",
        "parameters": [
          {
            "name": "collectionId",
            "in": "path",
            "description": "Id of the collection from which the item should be removed.",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "itemType",
            "in": "path",
            "description": "Item type, such as Project, Image, or Font.",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/ItemType"
            }
          },
          {
            "name": "itemId",
            "in": "path",
            "description": "Id of the item to remove.",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Success."
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Not authorized.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Items/BulkInsert": {
      "put": {
        "tags": [
          "Items"
        ],
        "summary": "Bulk insert many items into 1 or more collections.",
        "description": "Sample request\r\n\r\n    PUT /v1/BulkInsert\r\n    {\r\n        \"collectionIds\": [ 3fa85f64-5717-4562-b3fc-2c963f66afa6 ],\r\n        \"items\":[\r\n            { \"itemType\": \"Project\", \"itemId\": \"5ecd4ecd8252320d08ad8453\" },\r\n            { \"itemType\": \"Image\", \"itemId\": \"1234567\" },\r\n        ]\r\n    }",
        "operationId": "BulkInsertIntoCollections",
        "requestBody": {
          "description": "A list of items and collectionIds into which the items should be inserted.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BulkInsertRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BulkInsertResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Not authorized.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No items found with the given ids.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Items/BulkRemove": {
      "post": {
        "tags": [
          "Items"
        ],
        "summary": "Bulk remove many items from 1 or more collections.",
        "description": "Sample request\r\n\r\n    POST /v1/BulkRemove\r\n    {\r\n        \"collectionIds\": [ 3fa85f64-5717-4562-b3fc-2c963f66afa6 ],\r\n        \"items\":[\r\n            { \"itemType\": \"Project\", \"itemId\": \"5ecd4ecd8252320d08ad8453\" },\r\n            { \"itemType\": \"Image\", \"itemId\": \"1234567\" },\r\n        ]\r\n    }",
        "operationId": "BulkRemoveFromCollections",
        "requestBody": {
          "description": "A list of items and collectionIds from which the items should be removed.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BulkUpdateRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BulkUpdateResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Not authorized.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No items found with the given ids.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/User/{cricutUserId}/Collections": {
      "get": {
        "tags": [
          "UserCollections"
        ],
        "summary": "Retrieves a list of collections. Pagination available.",
        "description": "Sample request\r\n            \r\n    GET /v1/User/10009061/Collections?numberOfLastAddedItems=10&token=1&size=100",
        "operationId": "GetCollectionsByCricutUserId",
        "parameters": [
          {
            "name": "numberOfLastAddedItems",
            "in": "query",
            "description": "Specifies the number of last added items to include for each collection.",
            "schema": {
              "maximum": 10,
              "minimum": 0,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "token",
            "in": "query",
            "description": "The offset to use for the results that will be returned.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "size",
            "in": "query",
            "description": "The maximum number of results to return in the page.",
            "schema": {
              "maximum": 200,
              "minimum": 1,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "cricutUserId",
            "in": "path",
            "description": "CricutUserId of the collection owner.",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionsViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "No collection found with the given project id.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/User/{cricutUserId}/Collections/{itemType}/{itemId}": {
      "get": {
        "tags": [
          "UserCollections"
        ],
        "summary": "Retrieves a paginated list of collections for the given circutUserId that contain the given item.\r\nPagination available.",
        "description": "Sample request\r\n            \r\n    GET /v1/User/10009061/Collections/collection/12345?numberOfLastAddedItems=0&token=1&size=100",
        "operationId": "GetUserCollectionsByItem",
        "parameters": [
          {
            "name": "itemType",
            "in": "path",
            "description": "The type of item to add/remove.",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/ItemType"
            }
          },
          {
            "name": "itemId",
            "in": "path",
            "description": "The unique identifier of the item in the collection.",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "numberOfLastAddedItems",
            "in": "query",
            "description": "Specifies the number of last added items to include for each collection.",
            "schema": {
              "maximum": 10,
              "minimum": 0,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "token",
            "in": "query",
            "description": "The offset to use for the results that will be returned.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "size",
            "in": "query",
            "description": "The maximum number of results to return in the page.",
            "schema": {
              "maximum": 200,
              "minimum": 1,
              "type": "integer",
              "format": "int32"
            }
          },
          {
            "name": "cricutUserId",
            "in": "path",
            "description": "CricutUserId of the collection owner.",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Request successful. Returns collectionsViewModel.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CollectionsViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ValidationProblemDetails"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CricutProblemDetails"
                }
              }
            }
          }
        }
      }
    }
  }
}

Can you explain in a little more detail. The example I provided is for single string ‘values’ in the format as you provided, as opposed to being able to process an entire JSON string.
Which are the values from the JSON you have provided which appear to not be working - it could be that the regex needs tweaking once I see those.

As you already know you have given me the solutions regarding fetching paths based on get key and put it in map, like below

json_output = [
      + "/v1/Collections/{collectionId}",
      + "/v1/Collections/{collectionId}/counts",
      + "/v1/User/{UserId}/Collections",
      + "/v1/User/{UserId}/Collections/{itemType}/{itemId}",
    ]

I am iterating over to change to uppercase below is not working as expected

"/v1/User/{UserId}/Collections/{itemType}/{itemId}",

I will try my level best to solve, if you have any solution kindly share

Thanks @ExtelligenceIT

Ahh this explains it. My ‘solution’ assumes there is only one instance of something in curly braces (Based upon the original sample strings)

If the requirement is to change all such instances of a substring within a string then it will need some rework. Let me see what I can do :upside_down_face:

I think the general idea of using regular expressions to “tokenize” the string is a good one.

The variation I would suggest is to write an expression that matches either a braced identifier or a series of characters unbroken by an opening brace using a disjunction. If you use that with regexall you will then get a list of “tokens” where some of them begin with two open braces. You can then use a for expression to transform that list so that any token with two leading open braces is converted to uppercase, and then concatenate the transformed tokens back together to get a single string as the result.

Terraform is not really designed for this sort of custom parsing task, but the regular expression functions are at least a reasonable escape hatch for simpler cases like this one.

I’ve decided to be a bit less ‘clever’ with this and do it without regex, which probably makes it more accessible :slight_smile:

The below should work with 0 or more {} instances in a string. It uses a split and join with the original for

locals {
  original = "/v1/User/{UserId}/Collections/{itemType}/{itemId}/fred"

 
  # converting any match starting with { and ending with } to uppercase
  result = join("/", tolist([for m in split("/",local.original) : startswith(m, "{") && endswith(m, "}") ? upper(m) : m]))
}

output "result" {
  value = local.result
}

Outputs:

result = "/v1/User/{USERID}/Collections/{ITEMTYPE}/{ITEMID}/fred"
1 Like

Could you remove word please @ExtelligenceIT . Thanks for understanding.

Solution is really helpful kindly remove word please

Oh indeed, if it’s guaranteed that the braced parts will always be the entire content of a slash-delimited segment then using the slashes to tokenize instead is considerably simpler!

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.