Skip to main content

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

json_to_schema-0.1.0.tar.gz (6.1 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-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

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

Hashes for json_to_schema-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e31b3bd81418ee27271bf80200aef96c24a122186f5dee7782fee99d17a5c5b2
MD5 576a4b32d13294cd086cf6319613363b
BLAKE2b-256 bdb4f1f8bdee805ec93ca368c73e545ed57271187433e2a6d030f2f20d9ec2f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for json_to_schema-0.1.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-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

Hashes for json_to_schema-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec467547f8b49ffc124f19157865cda265981b282bab1390833b433651ad147d
MD5 f38c8fbf479fe011c46e8d91e506aa66
BLAKE2b-256 02a0e683228df723e175f59b64393f37845eadc5f9913f10947b24bbb55bc86c

See more details on using hashes here.

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

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