[Python] How to read openapi json schema and extract API information using python

Discussão em 'Python' iniciado por Stack, Agosto 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a openapi json schema file. Using Python, I want to read that json file and extract all the APIs defined in it (including API definition, request headers, request body etc...) and execute all those APIs in a loop. API execution part I can handle but what I want to know is there any standard python library to read the openapi json spec to extract API information. If there is a standard python library could you please give me a sample code how to extract the API details out of this openapi json.

    example openapi json schema:

    {
    "openapi": "3.0.0",
    "info": {
    "title": "Cat Facts API",
    "version": "1.0"
    },
    "paths": {
    "/breeds": {
    "get": {
    "tags": [
    "Breeds"
    ],
    "summary": "Get a list of breeds",
    "description": "Returns a a list of breeds",
    "operationId": "getBreeds",
    "parameters": [
    {
    "name": "limit",
    "in": "query",
    "description": "limit the amount of results returned",
    "required": false,
    "schema": {
    "type": "integer",
    "format": "int64"
    }
    }
    ],
    "responses": {
    "200": {
    "description": "successful operation",
    "content": {
    "application/json": {
    "schema": {
    "type": "array",
    "items": {
    "$ref": "#/components/schemas/Breed"
    }
    }
    }
    }
    }
    }
    }
    },
    "/fact": {
    "get": {
    "tags": [
    "Facts"
    ],
    "summary": "Get Random Fact",
    "description": "Returns a random fact",
    "operationId": "getRandomFact",
    "parameters": [
    {
    "name": "max_length",
    "in": "query",
    "description": "maximum length of returned fact",
    "required": false,
    "schema": {
    "type": "integer",
    "format": "int64"
    }
    }
    ],
    "responses": {
    "200": {
    "description": "successful operation",
    "content": {
    "application/json": {
    "schema": {
    "$ref": "#/components/schemas/CatFact"
    }
    }
    }
    },
    "404": {
    "description": "Fact not found"
    }
    }
    }
    },
    "/facts": {
    "get": {
    "tags": [
    "Facts"
    ],
    "summary": "Get a list of facts",
    "description": "Returns a a list of facts",
    "operationId": "getFacts",
    "parameters": [
    {
    "name": "max_length",
    "in": "query",
    "description": "maximum length of returned fact",
    "required": false,
    "schema": {
    "type": "integer",
    "format": "int64"
    }
    },
    {
    "name": "limit",
    "in": "query",
    "description": "limit the amount of results returned",
    "required": false,
    "schema": {
    "type": "integer",
    "format": "int64"
    }
    }
    ],
    "responses": {
    "200": {
    "description": "successful operation",
    "content": {
    "application/json": {
    "schema": {
    "type": "array",
    "items": {
    "$ref": "#/components/schemas/CatFact"
    }
    }
    }
    }
    }
    }
    }
    }
    },
    "components": {
    "schemas": {
    "Breed": {
    "title": "Breed model",
    "description": "Breed",
    "properties": {
    "breed": {
    "title": "Breed",
    "description": "Breed",
    "type": "string",
    "format": "string"
    },
    "country": {
    "title": "Country",
    "description": "Country",
    "type": "string",
    "format": "string"
    },
    "origin": {
    "title": "Origin",
    "description": "Origin",
    "type": "string",
    "format": "string"
    },
    "coat": {
    "title": "Coat",
    "description": "Coat",
    "type": "string",
    "format": "string"
    },
    "pattern": {
    "title": "Pattern",
    "description": "Pattern",
    "type": "string",
    "format": "string"
    }
    },
    "type": "object"
    },
    "CatFact": {
    "title": "CatFact model",
    "description": "CatFact",
    "properties": {
    "fact": {
    "title": "Fact",
    "description": "Fact",
    "type": "string",
    "format": "string"
    },
    "length": {
    "title": "Length",
    "description": "Length",
    "type": "integer",
    "format": "int32"
    }
    },
    "type": "object"
    }
    }
    }
    }

    Continue reading...

Compartilhe esta Página