OpenAPI Schema to JSON Schema Converter

0.0.3 · maintenance · verified Sun Apr 12

This Python library provides a utility to convert OpenAPI 3.0 Schema Objects into compatible JSON Schema Draft 4 representations. It addresses key discrepancies between the two specifications, such as `nullable` handling and removal of OpenAPI-specific keywords, to enable validation with standard JSON Schema validators. This package is a direct Python port of the JavaScript library `mikunn/openapi-schema-to-json-schema` (v2.1.0).

Warnings

Install

Imports

Quickstart

Demonstrates converting a simple OpenAPI schema object, including handling `nullable` and an OpenAPI extension field `x-patternProperties`.

import json
from openapi_schema_to_json_schema import to_json_schema

openapi_schema = {
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "nullable": True,
        }
    },
    "x-patternProperties": {
        "^[a-z]+$": {
            "type": "number",
        }
    }
}

# Options can be passed to control conversion behavior
# For example, to enable conversion of 'x-patternProperties' to 'patternProperties'
options = {"supportPatternProperties": True}

converted_schema = to_json_schema(openapi_schema, options)
print(json.dumps(converted_schema, indent=2))

view raw JSON →