Skip to main content

JSON Schema generate Python types

kk Tools to generate Python types based on TypedDict from a JSON schema

Quick start

install:

python3 -m pip install --user jsonschema-gentypes

Convert a JSON schema to a Python file contains the types:

jsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>

Config file

You can also write a config file named jsonschema-gentypes.yaml with:

headers: >
  # Automatically generated file from a JSON schema
# Used to correctly format the generated file
callbacks:
  - - black
  - - isort
generate:
  - # JSON schema file path
    source: jsonschema_gentypes/schema.json
    # Python file path
    destination: jsonschema_gentypes/configuration.py
    # The name of the root element
    root_name: Config
    # Argument passed to the API
    api_arguments:
      additional_properties: Only explicit
    # Rename an element
    name_mapping: {}
    # The minimum Python version that the code should support. By default the
    # currently executing Python version is chosen. Note that the output
    # may require typing_extensions to be installed.
    python_version: '3.11'

And just run:

jsonschema-gentypes

Default

The default values are exported in the Python file, then you can do something like that:

value_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)

Limitations

Requires Python 3.8

See the issues with label "limitation".

Pre-commit hooks

This project provides pre-commit hooks to automatically generate the files.

repos:
  - repo: https://github.com/camptocamp/jsonschema-gentypes
    rev: <version> # Use the ref you want to point at
    hools:
      - id: jsonschema-gentypes
        files: |
          (?x)^(
              jsonschema-gentypes\.yaml|
              <schema_path>\.json
          )$

See also the pre_commit section in the configuration to run the pre-commit just after the generation, for example with:

pre_commit:
  enabled: true
  arguments:
    - --color=never

OpenAPI3

We can also generate types for OpenAPI3 schemas (automatically detected).

The result of our example in tests/openapi3.json can be used in pyramid with for example:

import pyramid.request
from pyramid.view import view_config
from openaoi3 import *

def open_api(func):
    def wrapper(request: pyramid.request.Request, **kwargs) -> Any:
        typed_request = {}
        try:
            typed_request{'request_body'} = request.json
        except Exception as e:
            pass
        typed_request{'path'} = request.matchdict
        typed_request{'query'} = request.params

        return = func(request, request_typed=typed_request, **kwargs)

    return wrapper


@view_config(route_name="route_name", renderer="json")
@open_api
def view(
  request: pyramid.request.Request,
  request_typed: OgcapiCollectionsCollectionidGet,
) -> OgcapiCollectionsCollectionidGetResponse:
    return {...}

Contributing

Install the pre-commit hooks:

pip install pre-commit
pre-commit install --allow-missing-config

The prospector tests should pass.

The code should be typed.

The code should be tested with pytests.

Release files for jsonschema-gentypes 2.13.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonschema-gentypes 2.13.0
File Size Uploaded
jsonschema_gentypes-2.13.0.tar.gz 34.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonschema-gentypes 2.13.0
File Interpreter ABI Platform
jsonschema_gentypes-2.13.0-py3-none-any.whl Python 3 none any Details

Total release size: 86.0 kB

Release files / jsonschema_gentypes-2.13.0.tar.gz

Download URL jsonschema_gentypes-2.13.0.tar.gz
Size 34.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1f1dfe5150162e9c9a6c46dae0c4595380ff852cb7cfe33bdf1038e2174ba3d8
BLAKE2b-256 checksum
How to use checksums
f73adefad7cfe46d6b080c6988df413d6ead4022fae4484378acf4e763894260
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / jsonschema_gentypes-2.13.0-py3-none-any.whl

Download URL jsonschema_gentypes-2.13.0-py3-none-any.whl
Size 51.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e9e9d82a273911e1bb4cab374699119f6a53c984e41623e773eba83e36076be
BLAKE2b-256 checksum
How to use checksums
74213f243eb77495b249149b295ca635f24f8df6fe1695fa6c03c1199d72e3c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

2.13.0 This release

2 release files

2.12.0

2 release files

2.11.0

2 release files

2.10.0

2 release files

2.9.3

2 release files

2.9.2

2 release files

2.9.0

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.2

2 release files

2.7.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.7.2

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

1 release file

1.3.2

2 release files

1.3.1

1 release file

1.3.0

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page