Skip to main content

pydantic-generator generates the pydantic model classes.

Project description

pydantic-generator

pydantic-generator generates a pydantic schema module from a json file.

install

pydantic-generates uses ast.unparse() and therefore only supports python 3.9+.

$ python3.9 -m pip install pydantic-generator
$ pydanticgen --help
usage: pydanticgen [-h] -i INPUT_ [-o OUTPUT]

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_, --input_ INPUT_, --input INPUT_
  -o OUTPUT, --output OUTPUT

example

$ ls
response.json
$ cat response.json
{
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}

# this command generates Response.json
$ pydanticgen -i response.json
$ ls
response.json Response.py
$ cat Response.py
from pydantic import BaseModel

class Response(BaseModel):

    class Menu(BaseModel):
        id: str
        value: str

        class Popup(BaseModel):

            class Menuitem(BaseModel):
                value: str
                onclick: str
            menuitem: list[Menuitem]
        popup: Popup
    menu: Menu

(This sample can be found at https://json.org/example.html.)

Project details


Download files

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

Files for pydantic-generator, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size pydantic_generator-1.0.0-py3-none-any.whl (7.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size pydantic-generator-1.0.0.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page