Skip to main content

json-to-schema

Build PyPI version Python versions License: MIT

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-bounds and --infer-all-enum.
  • Sets root schema metadata with --schema-id, --schema-title, and --schema-description.
  • Sets field-level metadata with --field-title and --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

Metadata

Release files for json-to-schema 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-to-schema 1.3.0
File Size Uploaded
json_to_schema-1.3.0.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-to-schema 1.3.0
File Interpreter ABI Platform
json_to_schema-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.7 kB

Release files / json_to_schema-1.3.0.tar.gz

Download URL json_to_schema-1.3.0.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aaaf95da3abd01cd8630203b9c98dad067bbe351c3561981baa7ee73e5b6fa30
BLAKE2b-256 checksum
How to use checksums
b32358ac92794bbfe9081461f6d329ef7a083e9f8757ae98f138d8e3d10b7941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 21, 2026.

Transparency log

Release files / json_to_schema-1.3.0-py3-none-any.whl

Download URL json_to_schema-1.3.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de4f916ba4ec20dba7fc79395254a87e2e7b14aac67bd95ca9a460f9c9636cb4
BLAKE2b-256 checksum
How to use checksums
deb315afcb6c400dab0b0020e57ffc720ae5c59221dbc7b7982c50eb1b58701f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page