json-to-schema is a Python tool that infers a JSON Schema from JSON data.
Use it to generate JSON Schema definitions quickly from real payloads.
It outputs schemas compatible with JSON Schema draft 2020-12.
- Infers types for objects, arrays, and scalars.
- Merges array item types across elements.
- Marks object properties as required based on the input instance.
- Optionally infers bounds for selected fields with
--infer-bounds. - Optionally infers enum values for selected fields with
--infer-enum. - Supports global inference with
--infer-all-boundsand--infer-all-enum. - Sets root schema metadata with
--schema-id,--schema-title, and--schema-description. - Sets field-level metadata with
--field-titleand--field-description. - Validates input JSON against a provided schema file with
--validate. - Emits schemas compliant with JSON Schema draft 2020-12.
- Python 3.8+
pip install json-to-schemaProvide an input JSON file (defaults to file.json) and print the inferred schema to stdout:
json-to-schemaSpecify a custom input file and write output to a schema file:
json-to-schema -i input.json -o schema.jsonPrint compact/minified output:
json-to-schema -i input.json --minifyInfer bounds:
json-to-schema -i input.json --infer-bounds age price tagsInfer enum values:
json-to-schema -i input.json --infer-enum status typeInfer bounds for all applicable fields:
json-to-schema -i input.json --infer-all-boundsInfer enum values for all applicable fields:
json-to-schema -i input.json --infer-all-enumSet root schema metadata:
json-to-schema -i input.json \
--schema-id https://example.com/schemas/product \
--schema-title "Product Schema" \
--schema-description "Schema for product payloads"Set field-level metadata (dot paths, with [] for array items):
json-to-schema -i input.json \
--field-title user.name="Full name" \
--field-description tags[]="Tag value"Validate a payload file against an existing schema:
json-to-schema -i payload.json --validate schema.jsonValidate piped stdin against a schema:
echo '{"name":"Widget","price":12.5}' | json-to-schema --validate schema.jsonYou can also pipe JSON directly into stdin:
echo '{"name":"Widget","price":12.5}' | json-to-schemaYou can also import and use this package directly in Python applications:
import json
from json_to_schema import infer_schema, SCHEMA_DRAFT
data = {
"name": "Widget",
"price": 12.5,
"tags": ["sale", "featured"],
"in_stock": True
}
schema = {
"$schema": SCHEMA_DRAFT,
**infer_schema(data)
}
print(json.dumps(schema, indent=2))Input (input.json):
{
"name": "Widget",
"price": 12.5,
"tags": ["sale", "featured"],
"in_stock": true
}Run:
json-to-schema -i input.json -o schema.jsonOutput (schema.json):
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"price": { "type": "number" },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"in_stock": { "type": "boolean" }
},
"required": ["in_stock", "name", "price", "tags"],
"additionalProperties": false
}python -m unittest discover -s tests