How to use for loop with if condition for dynamic json

Below is the json file
{
“openapi”: “3.0.1”,
“info”: {
“title”: “Feedback.Api”,
“version”: “1.12.0”
},
“servers”: [
{
“url”: “https://feedback
}
],
“paths”: {
“/v1/Surveys/{surveyId}”: {
“get”: {
“tags”: [
“Surveys”
],
“summary”: “Get survey by id”,
“operationId”: “GetSurveyById”,
“parameters”: [
{
“name”: “surveyId”,
“in”: “path”,
“required”: true,
“schema”: {
“type”: “string”,
“format”: “uuid”
}
}
],
},
“/v1/Surveys/{surveyId}/summary”: {
“get”: {
“tags”: [
“Surveys”
],
“summary”: “Get survey summary by survey id”,
“operationId”: “GetSurveySummaryById”,
“parameters”: [
{
“name”: “surveyId”,
“in”: “path”,
“required”: true,
“schema”: {
“type”: “string”,
“format”: “uuid”
}
}
],
“/v1/Surveys”: {
“post”: {
“tags”: [
“Surveys”
],
“summary”: “Create user survey”,
“operationId”: “UpdateUserSurvey”,
“requestBody”: {
“content”: {
“application/json”: {
“schema”: {
“$ref”: “#/components/schemas/UserSurveyViewModel”
}
}
}
}
}
}
Questions:

  1. I want to get all the paths ( url ) if they are matching get (excluding the post )
  2. Create new json variable as key value pair with key as path url which are matching get and summary with description as another attribute

Gettiing all the paths
locals {

api-url-paths = [for url in keys(local.swagger-apis.servers) : url]

}

I am able to get all paths but not paths( /v1/Surveys/{surveyId}/summary )which are matching get ( “get”: ) since paths values is dynamic and also the summary value ( “summary”: “Get survey summary by survey id”, )

hi @prasannap,

Could you edit your post to use the preformatted text for your code/JSON blocks as it will make it a lot easier to read by honouring your indentation etc. It will also make it possible to copy/paste the code out of the post if people want to reproduce the scenario from your code.

You can use 3x ‘backtick’ characters on the line before and after your code blocks:

locals {
fred = “123”
}

Is then formatted as:

locals {
   fred = "123"
}

Or you can use the ‘<>’ button in the toolbar after highlighting code (This is good if you want to show something inline with your text.

I’ll take a look at this for you then, as it is hard to interpret quickly in its current format.

Thanks in advance.

Thank you @ExtelligenceIT !! Solution is much appreciated. The point is need to filter to get all the URL from paths which are not matching “post”, also need to get summary of the api as variables with key

Thanks in Advance!! and the tip

Below is the json file
{
  "openapi": "3.0.1",
  "info": {
    "title": "Feedback",
    "version": "1.12.0"
  },
  "servers": [
    {
      "url": "https://feedback"
    }
  ],
  "paths": {
    "/v1/Surveys/{surveyId}": {
      "get": {
        "tags": [
          "Surveys"
        ],
        "summary": "Get survey by id",
        "operationId": "GetSurveyById",
        "parameters": [   
          {
            "name": "surveyId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string", 
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SurveyResponseViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Surveys/{surveyId}/summary": {
      "get": {
        "tags": [
          "Surveys"
        ],
        "summary": "Get survey summary by survey id",
        "operationId": "GetSurveySummaryById",
        "parameters": [
          {
            "name": "surveyId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SurveySummaryViewModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    },
    "/v1/Surveys": {
      "post": {
        "tags": [
          "Surveys"
        ],
        "summary": "Create user survey",
        "operationId": "UpdateUserSurvey",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UserSurveyViewModel"
              }
            }
          }
        },
        "responses": {
          "204": {
            "description": "No Content"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    }
  }
}

Any solutions ? Its very hard to get the path uri based on value as get

OK, take a look at the below. I am using the keys function along with the filtering elements capability of the for expression to only select paths that do not include post. Once that is in a local variable it is easy to extract just the paths using the keys() function, as well as the summaries using another for expression:

The below is based upon your provided JSON which I saved to a local file and read in and decode. But, of course, this source JSON could already be in a local variable, input variable or another resource’s attribute.

Code

# Read in the JSON file
data "local_file" "jsondata" {
  filename = "data.json"
}

locals {
  # Decode the JSON data
  data = jsondecode(data.local_file.jsondata.content)


  # create map of objects, key being the path, that do not have an attribute "post"
  non_post_paths = { for path, values in local.data.paths : path => values if !contains(keys(values), "post") }

  # Create a list of paths (The keys of the map above)
  paths = keys(local.non_post_paths)

  # Create a map of paths and their summaries
  paths_summary = { for path, values in local.non_post_paths : path => values.get.summary }

}

output "paths" {
  value = local.paths
}

output "paths_summary" {
  value = local.paths_summary
}

Example Outputs

Outputs:

paths = [
  "/v1/Surveys/{surveyId}",
  "/v1/Surveys/{surveyId}/summary",
]
paths_summary = {
  "/v1/Surveys/{surveyId}" = "Get survey by id"
  "/v1/Surveys/{surveyId}/summary" = "Get survey summary by survey id"
}

Hope that helps

Happy Terraforming

Thank you very much @ExtelligenceIT !!! Its helps lot to continue my investigation

No problem at all - please mark the relevant post as the solution - it helps others jump to it if they have a similar query and are searching.

Good Luck!