Skip to main content

A tool to infer JSON Schemas from JSON data.

Project description

json-to-schema

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.
  • 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

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 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

json_to_schema-1.0.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_to_schema-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file json_to_schema-1.0.0.tar.gz.

File metadata

  • Download URL: json_to_schema-1.0.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_to_schema-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9d979a6ab29c1a0d3a583de452f303937cfde1ea8ca9ab7c8c893322b3faf99f
MD5 3552ebe64ccdd6b41c92681c756a4e90
BLAKE2b-256 aaeb2a5392b2fb655d1618e7c5057388ce69a10534385df20e3bdc9d15bdc747

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_to_schema-1.0.0.tar.gz:

Publisher: publish.yml on isalin/json-to-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file json_to_schema-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: json_to_schema-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_to_schema-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 327d490f83d94a5a3270ff5433b4c3fc1b15cdd3b93c2b4a134b3298bd3ac60c
MD5 31fcb2f55d3bcd80288370e40d4f08e7
BLAKE2b-256 a056c943ca0ccc15407c0e779c46798339c46e3126e63f63bbfe73a0fa32a6d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_to_schema-1.0.0-py3-none-any.whl:

Publisher: publish.yml on isalin/json-to-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page