Convert your JSON files into Pydantic classes.
Project description
JsonPyd
JsonPyd is a Python tool that automatically generates Pydantic models from JSON schemas.
Installation
You can easily install it via pip:
pip install jsonpyd
Usage
Terminal Usage
JsonPyd can be used as a command-line tool:
jsonpyd /path/to/schema.json --force-optional True
This command will generate a .py
file containing Python classes based on the specified JSON schema. Optionally, --force-optional
parameter can be used to make all fields in the schema optional.
Commands
The tool's command-line interface allows you to specify options for generating Pydantic models using the following parameters:
# Specify the path to the JSON schema file
schema_path <path_to_schema>
# Option to apply snake_case to variable names
--apply_snake_case [true|false]
# Option to force all variables to be optional
--force_optional [true|false]
# Set the name for the output file, default is the current date in 'dd-mm-yyyy_schema' format
--file_name <file_name>
These parameters can be used to customize the behavior of the model generation to fit your project requirements.
Usage within a Project
JsonPyd can also be used within Python files in a project:
from jsonpyd import JsonPyd
# String containing a schema
my_schema = '''
{
"field1": "value1",
"field2": {
"nested_field": "value2"
}
}
'''
# Create models using JsonPyd
pkg = JsonPyd(schema=my_schema, options={"force_optional": True})
pkg.convert_to_py()
This creates Pydantic models based on the specified JSON schema and converts them into a .py
file.
Features
- Automatic Model Generation: Automatically generates Pydantic models based on JSON schema.
- Optional Fields: Allows making all fields optional within a specific schema structure.
- Pydantic Integration: The generated models can leverage all features provided by Pydantic.
Contribution and Feedback
For any contributions and feedback, please visit our GitHub page.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.