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.1.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.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_schema_parser-1.0.1.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.34.0 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.14.4

File hashes

Hashes for json_schema_parser-1.0.1.tar.gz
Algorithm Hash digest
SHA256 63ed74716f11ffd5cf09cd36cbf318a202228e5533f81a563f3eb87e5afc32d1
MD5 49c6c2d20b70b315966f59ea85ccf286
BLAKE2b-256 38811de50686269a88ca8b1a97a0db0786b956e93bf5f9b9227de2bbb5d70f0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_schema_parser-1.0.1-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.34.0 requests-toolbelt/1.0.0 tqdm/4.67.3 CPython/3.14.4

File hashes

Hashes for json_schema_parser-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8fe3af84d9f41176d1c383abe5ba3b2f4fb9bed166ab53628a024cfbdee070f9
MD5 d02ffd98bf1fd58d8112fc057a0757cf
BLAKE2b-256 a20e01d6d1f8173140e5309c51c06326d3ae926bea1e5bbcb6851f90365952eb

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