Skip to main content

Generate a JSON Schema from sample JSON — merge multiple samples (or NDJSON) with type + format inference and required-key detection. Zero dependencies.

Project description

mkschema

Generate a JSON Schema from real JSON — and feed it more than one sample. You have an API response, a config, a pile of log records, and you want a JSON Schema for validation, docs, or contract tests. Hand-writing it is tedious; most generators take a single example and over-fit it — every field marked required, types pinned to whatever that one record happened to contain. mkschema merges many samples: a field in every sample is required, a field in only some is optional, and differing types are unioned. Zero dependencies, no network.

pip install mkschema

$ printf '{"id":1,"name":"Ada","age":30}\n{"id":2,"age":30.5}\n' | mkschema --ndjson -

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "age":  { "type": "number" },        // 30 and 30.5 unioned to number
    "id":   { "type": "integer" },
    "name": { "type": "string" }
  },
  "required": ["age", "id"]               // name was missing from the 2nd sample, so it's optional
}

This is the Python build. A behavior-equivalent Node build is on npm: npx mkschema (https://github.com/jjdoor/mkschema).

Usage

mkschema sample.json                 # infer from one file
mkschema a.json b.json c.json        # merge several samples into one schema
mkschema --ndjson records.ndjson     # one sample per line (logs, exports)
cat response.json | mkschema -        # read a JSON value from stdin
mkschema users.json --title User --id https://ex.com/user.schema.json

Schema goes to stdout, so redirect it: mkschema data.json > schema.json.

What it infers

  • Typesnull, boolean, integer, number, string, array, object. Numbers are classified by value, so 5.0 is an integer and the Python and Node builds agree.
  • String formatdate-time, date, email, uuid, ipv4, uri (kept only when all samples of a field agree).
  • required — the intersection across samples: a key present in every sample. (One sample ⇒ everything required.)
  • Arraysitems is the merge of all element schemas, so [1, "x"] becomes { "type": ["integer", "string"] }.
  • Unions — a field that is an integer in one sample and a float in another becomes number; genuinely different types become a sorted type array.

Options

Flag Effect
--ndjson <src> Treat each line of <src> (a file, or - for stdin) as a separate sample
--title <name> Set the schema title
--id <uri> Set $id
- Read one JSON value from stdin
-v, --version · -h, --help

Notes

  • Output is draft 2020-12 JSON Schema, deterministic (properties and required are sorted) so it diffs cleanly in version control.
  • Same tool, two builds. A behavior-equivalent Node build is on npm (npx mkschema); use whichever your stack has.
  • It infers structure, not constraints — add your own minLength, enum, pattern, etc. afterward. mkschema gives you the scaffold from real data.

Exit codes

Code Meaning
0 schema written
2 error (no input, invalid JSON, unreadable file)

License

MIT

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

mkschema-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

mkschema-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file mkschema-0.1.0.tar.gz.

File metadata

  • Download URL: mkschema-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for mkschema-0.1.0.tar.gz
Algorithm Hash digest
SHA256 92da090320e22aa6d050efef6dabd02f71c0db405a1ed61f69e51a5b3a7fc6fa
MD5 1abd16c5bc412a3a4b4933444b29bb8c
BLAKE2b-256 2c810cf28433dfce8313c3fc853eaa55ef9966cbae598a242d53e9f634d3861d

See more details on using hashes here.

File details

Details for the file mkschema-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mkschema-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for mkschema-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07908289395a843ef6550cab857db87a9e278863f56ceddd619fd8e0b8a89619
MD5 01cf15c78ec7ab0d74f5478dd327663c
BLAKE2b-256 b2ea30319ecaadc05e05159678c4e5bf5a3f86a9f43a3e5fa559b90488cfc25e

See more details on using hashes here.

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