簡體   English   中英

如何使用python json從JSON文件中提取特定對象

[英]How to extract a a specific object from JSON file using python json

使用Python,我需要通過讀取JSON文件來提取所有API(只是API端點名稱)端點。

以下是示例JSON代碼,

{
  "swagger": "2.0",
  "info": {
    "title": "None",
    "description": "some thing over here sample content\n",
    "version": "0.1"
  },
  "produces": [
    "application/json"
  ],
  "basePath": "/busrouting",
  "schemes": [
    "https"
  ],
  "definitions": {
    "OAuth2": {
      "description": "some thing over here sample content\n",
      "type": "coauthor",
      "flow": "implicit",
      "authorization": "NONE",
      "scopes": {
        "Zero": "three",
        "One": "two"
      }
    }
  },
  "paths": {
    "/service-provider/details": {
      "get": {
        "description": "some thing over here sample content",
        "security": [
          {
            "OAuth2": [
              "None",
              "None"
            ]
          }
        ],
        "parameters": [
          {
            "name": "service provider",
            "in": "query",
            "description": "List of service-provider ID's",
            "required": false,
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Record limit. Default is 20",
            "required": false,
            "type": "integer"
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Record offset. Default is 0.",
            "required": false,
            "type": "integer"
          }
        ],
        "tags": [
          "Service Providers"
        ],
        "responses": {
          "200": {
            "description": "OK.",
            "headers": {
              "Link": {
                "description": "some thing over here sample content\n",
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "schema": {
              "type": "object",
              "properties": {
                "names": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/service-provider"
                  }
                }
              } #Sample add
            }
          },
          "400": {
            "description": "Bad Request"
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Zero"
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "$ref": "#/definitions/Error"
            }
          }
        }
      }
    },
    "/Bus/{bus-id}/names": {
      "get": {
        "description": "some thing over here sample content\n",
        "security": [
          {
            "OAuth2": [
              "None",
              "None"
            ]
          }
        ],
        "parameters": [
          {
            "name": "Bus-id",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "tags": [
          "BusNames"
        ],
        "responses": {
          "200": {
            "description": "OK.",
            "schema": {
              "$ref": "#/definitions/busnames"
            }
          },
          "400": {
            "description": "Bad Request"
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Balances not found"
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "$ref": "#/definitions/Error"
            }
          }
        }
      }
    },
    "/busoperator/{bus-id}/names/routes": {
      "get": {
        "description": "some thing over here sample content.\n",
        "security": [
          {
            "OAuth2": [
              "None",
              "None"
            ]
          }
        ],
        "parameters": [
          {
            "name": "bus-id",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Record limit. Default is 10",
            "required": false,
            "type": "integer"
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Record offset. Default is 0.",
            "required": false,
            "type": "integer"
          }
        ],
        "tags": [
          "bus route mapping"
        ],
        "responses": {
          "200": {
            "description": "Recent Route",
            "headers": {
              "Link": {
                "description": "some thing over here sample content.\n",
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "schema": {
              "type": "object",
              "properties": {
                "Route details": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/Route"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request"
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Not entitled to this account and its transactions"
          },
          "404": {
            "description": "User has no recent transactions"
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "$ref": "#/definitions/Error"
            }
          }
        }
      }
    },
    "/operator/{id}/route/path": {
      "get": {
        "description": "some thing over here sample content.\n",
        "security": [
          {
            "OAuth2": [
              "None",
              "None"
            ]
          }
        ],
        "parameters": [
          {
            "name": "bus-id",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "limit",
            "in": "query",
            "description": "Record limit. Default is 10",
            "required": false,
            "type": "integer"
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Record offset. Default is 0.",
            "required": false,
            "type": "integer"
          }
        ],
        "tags": [
          "Sample Reporting"
        ],
        "responses": {
          "200": {
            "description": "sample actions",
            "headers": {
              "Link": {
                "description": "some thing over here sample content\n",
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "schema": {
              "type": "object",
              "properties": {
                "Boisterous": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/salesperson"
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad Request"
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Not entitled to this account and its transactions"
          },
          "404": {
            "description": "User has no recent transactions"
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "$ref": "#/definitions/Error"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "sample": {
      "required": [
        "demo",
        "request"
      ],
      "description": "Holds identifying attributes for an sample\n",
      "properties": {
        "request": {
          "description": "some thing over here sample content\n",
          "type": "string"
        },
        "sample one": {
          "type": "string"
        },
        "sample two": {
          "type": "string"
        },
        "number": {
          "description": "some thing over here sample content",
          "type": "string"
        },
        "name": {
          "type": "string",
          "description": "some thing over here sample content"
        }
      }
    },
    "dummy": {
      "description": "some thing over here sample content\n",
      "properties": {
        "dummy": {
          "$ref": "#/definitions/samples"
        },
        "data": {
          "type": "string",
          "description": "sample data "
        },
        "country": {
          "type": "string",
          "description": "some thing over here sample content"
        },
        "dataone": {
          "$ref": "#/definitions/samples"
        },
        "error": {
          "$ref": "#/definitions/Error"
        },
        "links": {
          "description": "some thing over here sample content",
          "type": "array",
          "items": {
            "$ref": "#/definitions/Links"
          }
        }
      }
    },
    "reference": {
      "description": "only description\n",
      "properties": {
        "available": {
          "type": "number",
          "format": "double",
          "description": "some thing over here sample content"
        },
        "availableFormatted": {
          "type": "string",
          "description": "some thing over here sample content"
        },
        "held": {
          "type": "number",
          "format": "double",
          "description": "some thing over here sample content."
        },
        "formatted": {
          "type": "string",
          "description": "some thing over here sample content"
        },
        "asst": {
          "type": "string",
          "format": "date-time",
          "description": "Timestamp of , in UTC"
        },
        "sampler": {
          "type": "string",
          "format": "date-time",
          "description": "Timestamp of , in users preferred TZ"
        }
      }
    },
    "dummy": {
      "description": "Transaction done on an account",
      "required": [
        "because",
        "versioned",
        "postdates"
      ],
      "properties": {
        "tintype": {
          "description": "sample content",
          "type": "string"
        },
        "cringed": {
          "description": "some thing over here sample content",
          "type": "string",
          "menu": [
            "D",
            "C"
          ]
        },
        "reference": {
          "description": "Reference information",
          "type": "string"
        },
        "amount": {
          "description": "some thing over here sample content\n",
          "type": "number",
          "format": "double"
        },
        "formatted": {
          "description": "formatted according to user's preferences\n"
        },
        "post Date": {
          "type": "string",
          "format": "date-time",
          "description": "Date-time in UTC"
        },
        "postdate": {
          "type": "string",
          "format": "date-time",
          "description": "Post Date-time in user-preferred TZ"
        },
        "narrative": {
          "type": "string"
        }
      }
    },
    "Links": {
      "description": "Related Links for the resource\n",
      "properties": {
        "rel": {
          "description": "relationship to the resource",
          "type": "string"
        },
        "ref": {
          "description": "URL of the related link",
          "type": "string"
        }
      }
    },
    "Error": { Sample
      "description": "Error descriptor",
      "properties": {
        "code": {
          "type": "integer",
          "format": "intent"
        },
        "message": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "severity": {
          "type": "string"
        },
        "location": {
          "type": "string"
        }
      }
    }
  }
}

我嘗試了以下代碼:

import json
with open("example.json", "r") as reading:
        data = json.load(reading)
        print(data["paths"])

從這里開始,我需要進一步執行代碼以僅捕獲所有API端點名稱。

在示例json文件中,在路徑下,我需要捕獲如下所示的API的所有端點和方法類型,

另外,我還想從示例JSON代碼中捕獲以下鍵的值。

在“參數”下,我還需要捕獲以下鍵,名稱:in:必需:

預期輸出(僅作為示例):

/service-provider/details
get
bus (comment - refers to the key - name under 'parameters')
query (comment - refers to the key - in under 'parameters')
false (comment - refers to the key - required under 'parameters')

/Bus/{bus-id}/names
get
bus-id (comment - refers to the key - name under 'parameters')
query (comment - refers to the key - in under 'parameters')
false (comment - refers to the key - required under 'parameters')

/busoperator/{bus-id}/names/routes
get
routes (comment - refers to the key - name under 'parameters')
query (comment - refers to the key - in under 'parameters')
false (comment - refers to the key - required under 'parameters')

/operator/{id}/route/path
get
id (comment - refers to the key - name under 'parameters')
query (comment - refers to the key - in under 'parameters')
false (comment - refers to the key - required under 'parameters')

要獲取兩個元組的列表(端點,類型),請使用:

[(endpoint, value.keys[0]) for endpoint, value in data["paths"].items()]

嘗試這個:

with open('example.json', 'r') as f:
    data = json.load(f)
    for path, values in data['paths'].items():
        print(path)
        for value in values:
            print(value)

如果有多個端點,則這還將使所有端點功能都在路徑中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM