{"id":2975,"library":"jschema-to-python","title":"JSON Schema to Python","description":"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.","status":"active","version":"1.2.3","language":"en","source_language":"en","source_url":"https://github.com/microsoft/jschema-to-python","tags":["json-schema","code-generation","data-modeling","schema-validation"],"install":[{"cmd":"pip install jschema-to-python","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"generate_code","correct":"from jschema_to_python import generate_code"},{"symbol":"generate_code_from_file","correct":"from jschema_to_python import generate_code_from_file"},{"symbol":"CodeGeneratorOptions","correct":"from jschema_to_python.code_generator_options import CodeGeneratorOptions"}],"quickstart":{"code":"import json\nfrom jschema_to_python import generate_code\nfrom jschema_to_python.code_generator_options import CodeGeneratorOptions\nimport os\n\n# Define a simple JSON schema for a Product\nschema = {\n    \"title\": \"Product\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"id\": {\"type\": \"string\", \"description\": \"Unique product identifier\"},\n        \"name\": {\"type\": \"string\"},\n        \"price\": {\"type\": \"number\"},\n        \"is_available\": {\"type\": \"boolean\"}\n    },\n    \"required\": [\"id\", \"name\", \"price\"]\n}\n\n# Configure generator options (optional - defaults are often fine)\noptions = CodeGeneratorOptions(\n    disable_formatter=False, # Keep formatting enabled by default\n    use_union_for_required_and_optional=True # Use Union[Type, None] for optional fields\n)\n\n# Generate Python code as a string\nmodule_name = 'product_model'\ngenerated_code_string = generate_code(schema, module_name, options)\n\n# To make the generated code importable, write it to a .py file\noutput_filename = f'{module_name}.py'\nwith open(output_filename, 'w') as f:\n    f.write(generated_code_string)\n\nprint(f\"Generated code successfully written to {output_filename}:\")\nprint(\"---\\n\" + generated_code_string + \"\\n---\")\n\n# You can now import and use the generated class:\n# from product_model import Product\n# my_product = Product(id=\"P123\", name=\"Example Item\", price=29.99, is_available=True)\n# print(my_product.to_dict())\n\n# Clean up the generated file for demonstration purposes (uncomment to enable)\n# os.remove(output_filename)\n","lang":"python","description":"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."},"warnings":[{"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.","message":"The 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+.","severity":"gotcha","affected_versions":"<=1.2.3"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"<=1.2.3"},{"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.","message":"By 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.","severity":"gotcha","affected_versions":"<=1.2.3"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}