Skip to main content

Infer JSON schemas from sample data

Project description

philiprehberger-schema-infer

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

Infer JSON schemas from sample data.

Installation

pip install philiprehberger-schema-infer

Usage

Infer schema from samples

from philiprehberger_schema_infer import infer

samples = [
    {"name": "Alice", "age": 30, "active": True},
    {"name": "Bob", "age": 25, "email": "bob@test.com"},
]

schema = infer(samples)
# {
#   "type": "object",
#   "properties": {
#     "name": {"type": "string", "minLength": 3, "maxLength": 5},
#     "age": {"type": "integer", "minimum": 25, "maximum": 30},
#     "active": {"type": "boolean"},
#     "email": {"type": "string", "format": "email", ...}
#   },
#   "required": ["age", "name"]
# }

Full JSON Schema output

from philiprehberger_schema_infer import to_json_schema

schema = to_json_schema(samples)
# {
#   "$schema": "https://json-schema.org/draft/2020-12/schema",
#   "type": "object",
#   "properties": { ... },
#   "required": [...]
# }

Single value type inference

from philiprehberger_schema_infer import infer_type

infer_type([1, 2, 3])
# {"type": "array", "items": {"type": "integer"}}

Merge schemas

from philiprehberger_schema_infer import merge_schemas

merged = merge_schemas(schema_a, schema_b)

API

Name Description
infer(samples) Infer JSON Schema from a list of dicts. Includes minimum/maximum for numbers and minLength/maxLength for strings.
infer_type(value) Infer schema type for a single value
merge_schemas(a, b) Merge two schemas into one accepting either
to_json_schema(samples) Wraps infer() output with $schema URI for draft 2020-12

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this package useful, consider starring the repository.

LinkedIn More packages

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

philiprehberger_schema_infer-0.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_schema_infer-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_schema_infer-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_schema_infer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6ddd9d74c0daf15fc2d7975a4ff581419e2628c05fe03b715be674b2a1972dfb
MD5 b63396cc0a007477a8f41d378efdec53
BLAKE2b-256 84262f21dbee02115e03238f7ce1528de04e9078432e26cc2c1c5260bd55556e

See more details on using hashes here.

File details

Details for the file philiprehberger_schema_infer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_schema_infer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb46191441a3cd1a1f64b0e58888e80c2e998b700aa24d7538f6f5c4978fd326
MD5 f2b3d132adda91b1850fefecfd8200b2
BLAKE2b-256 e06bc8d15ad7e0f8a74f88f6c5f64555d04b3ccce300b5e9c46e22190b76ca7c

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