Registry / serialization / jschema-to-python

jschema-to-python

JSON →
library1.2.3pypypi✓ verified 23d ago

jschema-to-python is a utility that generates Python source code for data classes directly from a JSON schema. As of version 1.2.3, it provides functionality to translate schema definitions into Python classes, complete with type hints and properties. The library, maintained by Microsoft, has an irregular release cadence, typically driven by internal needs or significant updates.

pip install jschema-to-python
INSTALL
IMPORT
SIG · JSCHEMA-TO-PYTHON
J
jschema-to-python
serializationpythonv1.2.3
Install
2.1s avg
Import
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.1s · import 0.000s · 20MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generate_code
from jschema_to_python import generate_code
from jschema_to_python import generate_code

This quickstart demonstrates how to define a JSON schema, use `jschema_to_python.generate_code` to convert it into a Python class string, and then explicitly write that string to a `.py` file. It also shows how to configure `CodeGeneratorOptions` to influence the generated code's style.

import json from jschema_to_python import generate_code from jschema_to_python.code_generator_options import CodeGeneratorOptions import os # Define a simple JSON schema for a Product schema = { "title": "Product", "type": "object", "properties": { "id": {"type": "string", "description": "Unique product identifier"}, "name": {"type": "string"}, "price": {"type": "number"}, "is_available": {"type": "boolean"} }, "required": ["id", "name", "price"] } # Configure generator options (optional - defaults are often fine) options = CodeGeneratorOptions( disable_formatter=False, # Keep formatting enabled by default use_union_for_required_and_optional=True # Use Union[Type, None] for optional fields ) # Generate Python code as a string module_name = 'product_model' generated_code_string = generate_code(schema, module_name, options) # To make the generated code importable, write it to a .py file output_filename = f'{module_name}.py' with open(output_filename, 'w') as f: f.write(generated_code_string) print(f"Generated code successfully written to {output_filename}:") print("---\n" + generated_code_string + "\n---") # You can now import and use the generated class: # from product_model import Product # my_product = Product(id="P123", name="Example Item", price=29.99, is_available=True) # print(my_product.to_dict()) # Clean up the generated file for demonstration purposes (uncomment to enable) # os.remove(output_filename)
jschema-to-python --version
Debug
Known issues
gotchaThe PyPI metadata specifies `requires_python >= 2.7`, implying the generator itself can run on older Python interpreters. However, for optimal compatibility with modern type-hinting, language features, and to avoid potential issues, it is strongly recommended to run `jschema-to-python` with Python 3.8+.
fix
Ensure your environment uses Python 3.8 or newer when generating code with this library to leverage contemporary Python features and maintain future compatibility.
affects: <=1.2.3
gotchaThe `generate_code` function returns the Python code as a plain string. It does not automatically write this string to a file or manage your project's file structure. Users must explicitly save the output to a `.py` file for it to be importable and runnable as a module.
fix
After calling `generate_code`, use standard Python file I/O operations (e.g., `with open('your_module_name.py', 'w') as f: f.write(generated_code_string)`) to persist the generated code.
affects: <=1.2.3
gotchaBy default, `jschema-to-python` attempts to format the generated code using a code formatter (like Black, if available). If you experience unexpected formatting, conflicts with your project's existing linters, or prefer manual formatting, you can disable this behavior using `CodeGeneratorOptions(disable_formatter=True)`. This will result in unformatted, but functionally correct, code.
fix
Pass `CodeGeneratorOptions(disable_formatter=True)` to the `generate_code` or `generate_code_from_file` function if you need to control the formatting externally or prefer unformatted output.
affects: <=1.2.3
Upgrade
Version history
1.2.3latest on PyPI · released Oct 5, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
jschema-to-python — pip install jschema-to-python · libregistry