Skip to main content

Convert your JSON files into Pydantic classes.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonpyd-0.2.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonpyd-0.2.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file jsonpyd-0.2.1.tar.gz.

File metadata

  • Download URL: jsonpyd-0.2.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-37-generic

File hashes

Hashes for jsonpyd-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fa34916cb14079fe1cfab1e44704d97eac6a923d9973eff647895fd3eeba714f
MD5 f1358ff079ebd06a9fd248f5bf22fd10
BLAKE2b-256 2705286371c9c82e00a70bc6379045a6b0923f65f3e5a424387eef13cf05e34b

See more details on using hashes here.

File details

Details for the file jsonpyd-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: jsonpyd-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-37-generic

File hashes

Hashes for jsonpyd-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d20d605e55367cbffd4d86dc31ca65a85f2b750991f6dc2c0d7bd16810670ba8
MD5 faef5f34e58d891d3ddf418832abcd73
BLAKE2b-256 2749baa981839aabdb277111a8daca445d6c881a1d3e7060d0326156e1e204b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page