Skip to main content

jsonpyd

Generate related pydantic class with any json type.

GitHub Repo stars GitHub issues GitHub

JsonPyd

JsonPyd is a 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

my_schema = '''
{
        "id": "1NW2ZvKYlo2CPkIVYDHw",
        "object": "payment_method",
        "price": 35.5312,
        "price_str": null,
        "billing_details": {
            "address": {
                "city": null,
                "country": "TR",
                "line1": null
            },
            "email": "jenny@example.com"
        }
    }
'''

# Create models using JsonPyd
pkg = JsonPyd(schema=my_schema, options={"force_optional": True})


# You can get Pydantic model by calling .model attribute.
pkg.model

# Or you can create .py file
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 my GitHub page or reach me via email.

Release files for jsonpyd 0.2.1

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

Source distribution (sdist)

Source distribution for jsonpyd 0.2.1
File Size Uploaded
jsonpyd-0.2.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonpyd 0.2.1
File Interpreter ABI Platform
jsonpyd-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / jsonpyd-0.2.1.tar.gz

Download URL jsonpyd-0.2.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fa34916cb14079fe1cfab1e44704d97eac6a923d9973eff647895fd3eeba714f
BLAKE2b-256 checksum
How to use checksums
2705286371c9c82e00a70bc6379045a6b0923f65f3e5a424387eef13cf05e34b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-37-generic

Release files / jsonpyd-0.2.1-py3-none-any.whl

Download URL jsonpyd-0.2.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d20d605e55367cbffd4d86dc31ca65a85f2b750991f6dc2c0d7bd16810670ba8
BLAKE2b-256 checksum
How to use checksums
2749baa981839aabdb277111a8daca445d6c881a1d3e7060d0326156e1e204b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-37-generic

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

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