jsonschema-typed-v2

raw JSON →
0.8.0 verified Fri May 01 auth: no python maintenance

Automatic type annotations from JSON schemas. Generate Python dataclasses with type hints from JSON Schema definitions. Version 0.8.0 is the latest. Release cadence is irregular; last release was in 2020. Requires Python >=3.7.

pip install jsonschema-typed-v2
error ImportError: cannot import name 'from_schema' from 'jsonschema_typed'
cause Attempting to import 'from_schema' which does not exist; the correct API is the 'JSONSchema' decorator.
fix
Use 'from jsonschema_typed import JSONSchema' and apply @JSONSchema to your class.
error jsonschema.exceptions.ValidationError: ... is not of type 'object'
cause The generated class expects an object schema, or the provided data does not match the schema.
fix
Ensure your JSON schema defines 'type': 'object' and that you pass valid data. Also check that required properties are present.
gotcha The library uses a docstring based JSON schema. The schema must be a valid JSON string inside the docstring of the class.
fix Ensure the docstring contains valid JSON without trailing commas, and is properly indented.
deprecated The package name on PyPI is 'jsonschema-typed-v2', but the import is 'jsonschema_typed'. Users often install the wrong package or try to import using hyphens.
fix Install with pip install jsonschema-typed-v2, then import with from jsonschema_typed import ...
gotcha Does not support JSON Schema drafts beyond draft-07. Features like if/then/else or 2020-12 vocabulary may not work.
fix Convert schema to draft-07 or use another library.

Define a dataclass from a JSON schema docstring.

from jsonschema_typed import JSONSchema

@JSONSchema
class MySchema:
    '''
    {
      "type": "object",
      "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"}
      },
      "required": ["name"]
    }
    '''
    pass

obj = MySchema(name="Alice", age=30)
print(obj.name, obj.age)