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
- Types —
null,boolean,integer,number,string,array,object. Numbers are classified by value, so5.0is anintegerand the Python and Node builds agree. - String
format—date-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.)- Arrays —
itemsis 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 sortedtypearray.
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
requiredare 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
Release files for mkschema 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mkschema-0.1.0.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mkschema-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:16.5 kB
Release files / mkschema-0.1.0.tar.gz
| Download URL | mkschema-0.1.0.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
92da090320e22aa6d050efef6dabd02f71c0db405a1ed61f69e51a5b3a7fc6fa
|
|
BLAKE2b-256 checksum How to use checksums |
2c810cf28433dfce8313c3fc853eaa55ef9966cbae598a242d53e9f634d3861d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Release files / mkschema-0.1.0-py3-none-any.whl
| Download URL | mkschema-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
07908289395a843ef6550cab857db87a9e278863f56ceddd619fd8e0b8a89619
|
|
BLAKE2b-256 checksum How to use checksums |
b2ea30319ecaadc05e05159678c4e5bf5a3f86a9f43a3e5fa559b90488cfc25e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|