A tool to infer JSON Schemas from JSON data.
Project description
json-to-schema
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.
Features
- 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.
Requirements
- Python 3.8+
Install
pip install json-to-schema
Generate JSON Schema from a JSON file (CLI)
Provide an input JSON file (defaults to file.json) and print the inferred schema to stdout:
json-to-schema
Specify a custom input file and write output to a schema file:
json-to-schema -i input.json -o schema.json
Print compact/minified output:
json-to-schema -i input.json --minify
Infer bounds:
json-to-schema -i input.json --infer-bounds age price tags
Infer enum values:
json-to-schema -i input.json --infer-enum status type
Infer bounds for all applicable fields:
json-to-schema -i input.json --infer-all-bounds
Infer enum values for all applicable fields:
json-to-schema -i input.json --infer-all-enum
Set 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.json
Validate piped stdin against a schema:
echo '{"name":"Widget","price":12.5}' | json-to-schema --validate schema.json
You can also pipe JSON directly into stdin:
echo '{"name":"Widget","price":12.5}' | json-to-schema
Infer JSON Schema in Python code (library usage)
You 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))
Example: convert sample JSON to schema
Input (input.json):
{
"name": "Widget",
"price": 12.5,
"tags": ["sale", "featured"],
"in_stock": true
}
Run:
json-to-schema -i input.json -o schema.json
Output (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
}
Testing
python -m pip install -e .
python -m unittest discover -s tests
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_to_schema-1.2.0.tar.gz.
File metadata
- Download URL: json_to_schema-1.2.0.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdad58620a7e82f57f7dcad9cc85c3f32e8f0395bea9371a373324193954411a
|
|
| MD5 |
810f4f490096a0251490ef6c05c18f68
|
|
| BLAKE2b-256 |
e161a1b0cd5234164de86ddca57b9ae26c464307cbd2ca6bf4d98e6b082f6538
|
Provenance
The following attestation bundles were made for json_to_schema-1.2.0.tar.gz:
Publisher:
publish.yml on isalin/json-to-schema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_to_schema-1.2.0.tar.gz -
Subject digest:
cdad58620a7e82f57f7dcad9cc85c3f32e8f0395bea9371a373324193954411a - Sigstore transparency entry: 1138867430
- Sigstore integration time:
-
Permalink:
isalin/json-to-schema@8372202647b2bc6cf051453d7e840462ed4afaa8 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/isalin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8372202647b2bc6cf051453d7e840462ed4afaa8 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file json_to_schema-1.2.0-py3-none-any.whl.
File metadata
- Download URL: json_to_schema-1.2.0-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78dfce780f7ab3cd338b9b0cb5f40aa6014189d46ce23281bc1c41142086a635
|
|
| MD5 |
48520b06acb21e0acfb9003a5657b924
|
|
| BLAKE2b-256 |
8d6192245dd5c07f8431bca8ebd1183aebeae439e8d8b44676f843b1aef9a57f
|
Provenance
The following attestation bundles were made for json_to_schema-1.2.0-py3-none-any.whl:
Publisher:
publish.yml on isalin/json-to-schema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
json_to_schema-1.2.0-py3-none-any.whl -
Subject digest:
78dfce780f7ab3cd338b9b0cb5f40aa6014189d46ce23281bc1c41142086a635 - Sigstore transparency entry: 1138867497
- Sigstore integration time:
-
Permalink:
isalin/json-to-schema@8372202647b2bc6cf051453d7e840462ed4afaa8 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/isalin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8372202647b2bc6cf051453d7e840462ed4afaa8 -
Trigger Event:
workflow_dispatch
-
Statement type: