Skip to main content

Convert between JSON schema definitions and Pydantic models

Project description

json-schema-parser

A Python library for converting between JSON schema definitions and Pydantic models.

Features

  • Parse a JSON schema dict into a validated Pydantic model
  • Generate a JSON schema dict from any Python type or Pydantic model
  • Supports all common types: integer, float, string, boolean, json, uuid, datetime
  • Supports list (is_many) and optional (is_optional) variants of every type
  • Nested object schemas via ComplexTypeInput

Installation

pip install json-schema-parser

Usage

Parse a JSON schema into a Pydantic model

from json_schema_parser import JsonSchemaParser

schema = {
    "__config": {"field_type": "json", "is_many": False, "is_optional": False, "field_default_value": None, "description": ""},
    "name": {"field_type": "string", "is_many": False, "is_optional": False, "field_default_value": None, "description": "User name"},
    "age":  {"field_type": "integer", "is_many": False, "is_optional": True, "field_default_value": None, "description": "User age"},
}

parser = JsonSchemaParser.model_validate(schema)
Model = parser.generate_pydantic_model("User")

user = Model.model_validate({"name": "Alice", "age": 30})

Generate a JSON schema from a Python type

from json_schema_parser import JsonSchemaParser
from pydantic import BaseModel
from typing import Optional, List

class Address(BaseModel):
    street: str
    city: str

schema = JsonSchemaParser.generate_json_schema_from_data_type(Address)

Supported field types

field_type value Python type
"integer" int
"float" float
"string" str
"boolean" bool
"uuid" UUID
"datetime" datetime
"json" dict / nested model

Set is_many: true to get a List[T], and is_optional: true to get Optional[T].

License

MIT

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

json_schema_parser-1.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

json_schema_parser-1.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file json_schema_parser-1.0.0.tar.gz.

File metadata

  • Download URL: json_schema_parser-1.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/9.0.0 pkginfo/1.12.1.2 requests/2.33.1 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.14.4

File hashes

Hashes for json_schema_parser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d9bfdb9b50d5a76956f8d1055c91a674d368d062b28a95c095c05ad5dea830e0
MD5 e2c13e5af753c6931de0b100038bd194
BLAKE2b-256 82d9b579ba40f3f40345a54fbc6120adbd226ec452c1a7272e0c21f82d11aff4

See more details on using hashes here.

File details

Details for the file json_schema_parser-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: json_schema_parser-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/9.0.0 pkginfo/1.12.1.2 requests/2.33.1 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.14.4

File hashes

Hashes for json_schema_parser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f779bc2430e0b5f2852fb68ee19e72d21ad8e714789059b711358f6f5bfbbdcd
MD5 1b5240ea6d2e6bf2d4d9feed09ca000f
BLAKE2b-256 b2fd16df7fa1bbbda807738e9b84146cedd0ccd233ca85245dd85c45c98fceaa

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