Generate a JSON Schema (draft 2020-12) from a sample JSON instance.
Project description
json-to-schema
Generate a JSON Schema (based on draft 2020-12) from a sample JSON instance.
Features
- Infers types for objects, arrays, and scalars.
- Merges array item types across elements.
- Marks object properties as required based on the input instance.
- Emits schemas compliant with JSON Schema draft 2020-12.
Requirements
- Python 3.8+
Install
pip install json-to-schema
CLI Usage
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
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
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 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-0.1.0.tar.gz.
File metadata
- Download URL: json_to_schema-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e31b3bd81418ee27271bf80200aef96c24a122186f5dee7782fee99d17a5c5b2
|
|
| MD5 |
576a4b32d13294cd086cf6319613363b
|
|
| BLAKE2b-256 |
bdb4f1f8bdee805ec93ca368c73e545ed57271187433e2a6d030f2f20d9ec2f1
|
Provenance
The following attestation bundles were made for json_to_schema-0.1.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-0.1.0.tar.gz -
Subject digest:
e31b3bd81418ee27271bf80200aef96c24a122186f5dee7782fee99d17a5c5b2 - Sigstore transparency entry: 927047239
- Sigstore integration time:
-
Permalink:
isalin/json-to-schema@c8c2a9a255b0a8cb87a3b0e86705fc7775ff0754 -
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@c8c2a9a255b0a8cb87a3b0e86705fc7775ff0754 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file json_to_schema-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json_to_schema-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.1 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 |
ec467547f8b49ffc124f19157865cda265981b282bab1390833b433651ad147d
|
|
| MD5 |
f38c8fbf479fe011c46e8d91e506aa66
|
|
| BLAKE2b-256 |
02a0e683228df723e175f59b64393f37845eadc5f9913f10947b24bbb55bc86c
|
Provenance
The following attestation bundles were made for json_to_schema-0.1.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-0.1.0-py3-none-any.whl -
Subject digest:
ec467547f8b49ffc124f19157865cda265981b282bab1390833b433651ad147d - Sigstore transparency entry: 927047241
- Sigstore integration time:
-
Permalink:
isalin/json-to-schema@c8c2a9a255b0a8cb87a3b0e86705fc7775ff0754 -
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@c8c2a9a255b0a8cb87a3b0e86705fc7775ff0754 -
Trigger Event:
workflow_dispatch
-
Statement type: