GenSON
GenSON is a powerful, user-friendly JSON Schema generator built in Python. Its core function is to take JSON objects and generate schemas that describe them, and it is unique in its ability to merge schemas. It was originally built to describe the common structure of a large number of JSON objects. The current version is 1.3.0, released on May 15, 2024, and the library is actively maintained.
Warnings
- gotcha This `genson` library is *not* the Python equivalent of the Java 'Genson' library. If you are looking for Java's Genson features in Python, this is not the correct library. Consider Python's built-in `json` library for basic JSON handling.
- gotcha GenSON infers types and marks *all observed fields as required* by default. If a field is present in some objects but not others, it will be made optional in the resulting schema. However, if a field is always present, it will be marked as 'required'.
- gotcha GenSON only deals with a *subset of JSON Schema keywords* for inference (e.g., "$schema", "type", "items", "properties", "patternProperties", "required", "anyOf"). It does not automatically infer or include semantic constraints like `enum` values (unless seeded), `minLength`, `maxLength`, `pattern` for strings, or `minItems`, `maxItems` for arrays.
- gotcha GenSON uses Python's flavor of regular expressions for `patternProperties`. Be aware of potential differences if you are accustomed to other regex engines. Also, `genson` prefers `properties` over `patternProperties`: if a property already exists that matches one of your patterns, the normal property will be updated, not the pattern property.
Install
-
pip install genson
Imports
- SchemaBuilder
from genson import SchemaBuilder
Quickstart
from genson import SchemaBuilder
import json
# Initialize SchemaBuilder
builder = SchemaBuilder()
# Add the first JSON object
builder.add_object({
"name": "Alice Johnson",
"email": "alice@example.com",
"age": 30,
"is_active": True,
"tags": ["user", "admin"]
})
# Add a second JSON object, demonstrating schema merging
builder.add_object({
"name": "Bob Smith",
"email": "bob@example.com",
"age": 25,
"phone": "+1-555-0100",
"tags": ["user"]
})
# Generate and print the unified JSON Schema
schema = builder.to_schema()
print(json.dumps(schema, indent=2))