Skip to main content

A library for comparing JSON and schemas and generate new

Project description

๐Ÿ” genschema

logo.webp

A powerful, intelligent library for generating JSON Schema from multiple JSON instances with smart merging, advanced inference, and modular refinements.

Tests Coverage Python PyPI - Package Version License BlackCode mypy

โญ Star us on GitHub | ๐Ÿ“š Read the Docs | ๐Ÿ› Report Bug

โœจ Features

  • ๐ŸŽฏ Intelligent Merging โ€“ Combines multiple JSON instances into a single schema
  • ๐Ÿ”— Configurable Combinators โ€“ Use anyOf or oneOf for conflicting types/properties
  • ๐Ÿง  Advanced Inference โ€“ Automatic format detection (email, uuid, date-time, etc.)
  • ๐Ÿท๏ธ Enum Inference โ€“ Promotes compact string fields to enum with safety guards
  • ๐Ÿ“ Required & Empty Handling โ€“ Smart inference of required, minProperties, minItems, etc.
  • ๐Ÿ” Pseudo-Array Detection โ€“ Treats inhomogeneous arrays as object-like structures when needed
  • โ™ป๏ธ Reference Extraction Postprocessing โ€“ Moves repeated or similar fragments into $defs / $ref
  • โšก Modular Pipeline โ€“ Chain of configurable comparators for full control
  • ๐Ÿ› ๏ธ CLI & Python API โ€“ Flexible usage from command line or code
  • ๐Ÿ“ Rich Output โ€“ Colored console feedback with timing and instance count

๐Ÿš€ Quick Start

Installation

pip install genschema

30-Second Python Example

from genschema import Converter, PseudoArrayHandler
from genschema.comparators import (
    EnumComparator,
    FormatComparator,
    RequiredComparator,
    EmptyComparator,
    DeleteElement,
)
from genschema.postprocessing import (
    SchemaReferenceExtractionConfig,
    SchemaReferencePostprocessor,
)

conv = Converter(
    pseudo_handler=PseudoArrayHandler(),
    base_of="anyOf",  # or "oneOf"
)

# Add JSON data (files, dicts, or existing schemas)
conv.add_json("example1.json")
conv.add_json("example2.json")
conv.add_json({"name": "Alice", "email": "alice@example.com"})

# Register optional refinements
conv.register(FormatComparator())  # Run format detection first
conv.register(EnumComparator())  # Then infer enum for short low-cardinality string fields
conv.register(RequiredComparator())
conv.register(EmptyComparator())
conv.register(DeleteElement())
conv.register(DeleteElement("isPseudoArray"))

# Generate schema
result = conv.run()

# Optional independent postprocessing:
# extract repeated / similar fragments into $defs + $ref
result = SchemaReferencePostprocessor.process(
    result,
    SchemaReferenceExtractionConfig(
        similarity_threshold=0.85,
        min_total_keys=3,
    ),
)

print(result)  # Pretty-printed JSON Schema

SchemaReferencePostprocessor is intentionally separate from Converter: it works on an already generated schema, including schemas built from a single JSON document if that document contains repeated or sufficiently similar structures.

CLI Usage

# Basic: single or multiple files
genschema input1.json input2.json -o schema.json

# Use oneOf instead of anyOf
genschema *.json --base-of oneOf -o schema.json

# Extract shared refs directly from CLI
genschema input.json --extract-refs -o schema.json

# Tune reference extraction
genschema input.json --extract-refs --refs-similarity-threshold 0.9 --refs-min-total-keys 4 -o schema.json

# Disable refinements
genschema data.json --no-format --no-enum --no-required --no-pseudo-array

# Read from stdin
cat data.json | genschema - -o schema.json

For advanced reference-extraction customization, the Python API still exposes more knobs than the CLI.

EnumComparator intentionally works only for string fields. For numeric columns it is technically hard to distinguish true enums from ordinary ids, years, counters, indexes, and external codes with acceptable reliability.

๐Ÿ“Š Comparison with GenSON

Feature genschema GenSON
Multiple Instance Merging Yes Yes
Variant Type Handling Configurable anyOf or oneOf anyOf only
Format Inference Yes (email, date-time, uuid, uri, etc.) No
Required Properties Configurable inference Yes (present in all objects)
Empty/Min-Max Handling Yes (minProperties, minItems, etc.) Limited
Pseudo-Array Detection Yes No
Modular Extensions Comparator pipeline (easy to add/remove) SchemaStrategy subclasses
CLI Support Full-featured with rich output Basic (genson)
Performance (avg. benchmark) ~2.1ร— slower Faster

Note: Performance measured on static datasets of varying complexity. genschema prioritizes richer inference and flexibility over raw speed.

๐Ÿ—๏ธ Architecture

Modular pipeline design for clean, extensible code:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Input JSONs   โ”‚      โ”‚  Input Schemas  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ–ผ
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ”‚ Pipeline Run  โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Process Layer    โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
                    โ”‚               โ”‚
                    โ–ผ               โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
        โ”‚ Comparators Chain   โ”‚โ”€โ”€โ”€โ”€โ”€โ”˜
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
                    โ–ผ
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ”‚    Result     โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Development

Setup

git clone https://github.com/Miskler/genschema.git
cd genschema
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -e ".[dev]"    # or make install-dev if Makefile exists

Common Commands

make test          # Run tests with coverage
make lint          # Lint code
make type-check    # mypy checking
make format        # Format with black
make docs          # Build documentation

๐Ÿ“š Documentation

๐Ÿค Contributing

We welcome contributions!

Fork the repository, create a feature branch, and submit a pull request.
Ensure tests pass and code follows black/mypy style.

make test
make lint
make type-check

๐Ÿ“„ License

AGPL-3.0 License โ€“ see LICENSE file for details.

Made with โค๏ธ for developers working with evolving JSON data

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

genschema-0.2.0.tar.gz (65.3 kB view details)

Uploaded Source

Built Distribution

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

genschema-0.2.0-py3-none-any.whl (53.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: genschema-0.2.0.tar.gz
  • Upload date:
  • Size: 65.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genschema-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d74c98ea1156965bac315d98f16b1fc404a342c86e08368d7d5a46e714cee667
MD5 a80f3e0384811349c86aafdf798e031e
BLAKE2b-256 cea460363a84e058c38cbfbf5b5e426258f86a4272e6b45647b7b9118e3fec64

See more details on using hashes here.

Provenance

The following attestation bundles were made for genschema-0.2.0.tar.gz:

Publisher: release.yml on Miskler/genschema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: genschema-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 53.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genschema-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5f911f549ea38038e7c3de55fc183cb943f227fd404ab6e86a52bdde66565a9
MD5 e6502805448bf0f673b6ca3156ec8b74
BLAKE2b-256 62af5700eaf9c5c4d9a4377a6972f34daf4bdf0a2856fded9894c594fdbab676

See more details on using hashes here.

Provenance

The following attestation bundles were made for genschema-0.2.0-py3-none-any.whl:

Publisher: release.yml on Miskler/genschema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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