JSONSchema Spec with object-oriented paths

0.4.5 · active · verified Sat Mar 28

jsonschema-path is a Python library that provides an object-oriented way to traverse and access JSONSchema definitions. It enhances JSON Schema handling by offering path-like navigation and on-demand dereferencing with a separate accessor layer. It is currently at version 0.4.5 and actively maintained with a regular release cadence, focusing on bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

Demonstrates creating a SchemaPath from a dictionary, traversing properties, dereferencing `$ref`s, and accessing schema content. It also shows how to enable the optional resolved-path cache for performance improvements.

from jsonschema_path import SchemaPath

d = {
    "properties": {
        "info": {
            "$ref": "#/definitions/Info"
        }
    },
    "definitions": {
        "Info": {
            "properties": {
                "title": {
                    "type": "string"
                },
                "version": {
                    "type": "string",
                    "default": "1.0"
                }
            }
        }
    }
}

path = SchemaPath.from_dict(d)

# Traverse schema like paths
assert "properties" in path

# Concatenate paths with /
info_path = path / "properties" / "info"
assert "properties" in info_path # Implicit dereferencing

version_path = info_path / "properties" / "version"

# Open content with implicit dereferencing
with version_path.open() as contents:
    print(contents)
# Expected output: {'type': 'string', 'default': '1.0'}

# Enable resolved-path LRU cache for performance
path_with_cache = SchemaPath.from_dict(d, resolved_cache_maxsize=64)

view raw JSON →