【问题标题】:Extraction of different paths starting with different identifiers in a JSON File在 JSON 文件中提取以不同标识符开头的不同路径
【发布时间】:2022-11-19 19:30:07
【问题描述】:

我有一个 json 文件,它包含不同的 API 规范,其中我对路径字段感兴趣,每个 API 都不同,我想提取这些以供我分析。但是有一些问题。

有些路径是这样开始的:

    "paths": {
      "/pipeline": {
        "404": "Not Found"
      },
      "/pipeline/{pipeline_id}": {
        "404": "Not Found"
      },
      "/pipeline/{pipeline_id}/config": {
        "404": "Not Found"
      },
      "/pipeline/{pipeline_id}/composer": {
        "404": "Not Found"
      },
      "/pipeline/{pipeline_id}/jenkinsfile": {
        "404": "Not Found"
      },
      "/pipeline/{pipeline_id}/run": {
        "404": "Not Found"
      }
 "paths": {
      "/convert": {
        "get": {
          "tags": [
            "Converter"
          ],
          "summary": "Convert a swagger definition",
          "description": "Converts the supplied payload to a 3.0 specification\nbased on a `url` parameter, which points to a older\nspecification version\n",
          "operationId": "convertByUrl",
          "parameters": [
            {
              "name": "url",
              "in": "query",
              "description": "A URL to the swagger definition",
              "required": true,
              "type": "string"
            }
          ],

而其他人是这样的:

"paths": {
      "/api/v1/pulses": {
        "404": "Not Found"
      },
      "/api/v1/pulses/{pulse-number}": {
        "404": "Not Found"
      },
      "/api/v1/jetdrops/{jetdrop-id}/records": {
        "404": "Not Found"
      },
      "/api/v1/lifeline/{object-reference}/records": {
        "404": "Not Found"
      }

我想提取它们,以便我可以对哪一个进行分类版本在它们中(如 api/v1),以及没有版本的。有什么办法可以在不获取参数的情况下提取版本,因为我不确定路径中有多少个版本。我不知道从哪里开始,所以任何帮助将不胜感激!

【问题讨论】:

  • 显示您的预期结果。

标签: python openapi


【解决方案1】:

像这样?使用过滤器。我不得不使用 JSON.loads 因为它似乎仍然是字符串格式

  1. 在 Python 对象中加载数据
    import json
    json_data = json.loads("""{
        "paths": {
            "/pipeline": {
                "404": "Not Found"
            },
            "/pipeline/{pipeline_id}": {
                "404": "Not Found"
            },
            "/pipeline/{pipeline_id}/config": {
                "404": "Not Found"
            },
            "/pipeline/{pipeline_id}/composer": {
                "404": "Not Found"
            },
            "/pipeline/{pipeline_id}/jenkinsfile": {
                "404": "Not Found"
            },
            "/pipeline/{pipeline_id}/run": {
                "404": "Not Found"
            },
            "/convert": {
                "get": {
                    "tags": [
                        "Converter"
                    ],
                    "summary": "Convert a swagger definition",
                    "description": "Converts the supplied payload to a 3.0 specification\nbased on a `url` parameter, which points to a older\nspecification version\n",
                    "operationId": "convertByUrl",
                    "parameters": [
                        {
                            "name": "url",
                            "in": "query",
                            "description": "A URL to the swagger definition",
                            "required": true,
                            "type": "string"
                        }
                    ]
                }
            },
            "/api/v1/pulses": {
                "404": "Not Found"
            },
            "/api/v1/pulses/{pulse-number}": {
                "404": "Not Found"
            },
            "/api/v1/jetdrops/{jetdrop-id}/records": {
                "404": "Not Found"
            },
            "/api/v1/lifeline/{object-reference}/records": {
                "404": "Not Found"
            }
        }
    }""")
    
    1. 提取以“/api/v1/”或“/convert”开头的所有内容:
    paths = json_data["paths"]
    start = ("/api/v1/", "/convert")
    new_paths = dict(filter(lambda item: item[0].startswith(start), paths.items()))
    print(json.dumps(new_paths, indent=4))
    
    1. 提取所有包含“/pulses”或“/jetdrops/”的内容:
    paths = json_data["paths"]
    key_words = ["/pulses", "/jetdrops/"]
    new_paths = dict(filter(lambda item: any(key_word in item[0] for key_word in key_words), paths.items()))
    print(json.dumps(new_paths, indent=4))
    

【讨论】:

  • 有点不同,因为我的这些路径在我的数据框中的一列下,而且我有几个关键字,比如要提取的不仅仅是 /api/v1。所以我能够提取路径,现在我需要过滤掉特定的关键字并将其作为新列打印出来,你对此有什么建议吗?
  • 你能给我提供更多信息吗?
猜你喜欢
  • 2013-05-25
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多