MyNixOS website logo
Description

JSON to JSON Schema.

A JSON Schema Draft 4 Generator from JSON Instances

JSONs to Schema

A JSON Schema Draft 4 Generator from JSON Instances

Suppose you have an API that produces complex and somewhat varied JSON. You'd like to be able to document that API in a standard format by observing only its output. JSON Schema is a reasonable format for that. But simply generating the schema for one document will lead to an incomplete description of the API since not all documents will have all the keys in the full document. This is where this library comes in.

CLI Overview

JSONs to Schema can also be used on the command line. After locating the installed binary, run it with --help to get detailed usage instructions.

API Overview

The main API function is jsonsToSchema along with its configuration-taking equivalent, jsonsToSchemaWithConfig. This will transform a set of JSON documents into a single Maybe schema.

Other helpful functions are jsonToSchema, which transforms a single document, and schemasToSchema, which performs the actual work of schema unification.

Properties

The schema generator should satisfy these properties:

  • An schema generated from a JSON document will always be able to validate that JSON document.
  • When a schema is merged with another schema, all objects that would validate under either schema, subject to the assumptions below, will validate under the merged schema regardless of merge order.

Unlike other generators strictness is meant to be customizable. Whether to allow additional properties in schema objects is something that the user can turn on and off.

Assumptions

When merging JSON documents the generator keeps track of only one possible underlying type for each primitive type. Thus, if there were two documents {"name": "x"} and {"cost": 24} they will be assumed to represent two different manifestations of a single document that has both the name and the cost keys, rather than two different documents.

If given an object and an array (or some other primitive type), however, the generator will be able to assume that the particular JSON document can be either an object or an array type.

Prior Art

This project was inspired by GenSON, a Python project that does much of the same, but aims to be more customizable, correct, and powerful.

Certain features were also inspired by schema-guru, but this project aims to be more customizable and have a programmatic API. Schema Guru's enum and range detection features are planned to be matched by this library.

Future Plans

  • Option to autogenerate enumerations for fields with below a certain limit of values
  • Option to automatically detect ranges for integers, strings, arrays, and objects

Not Planned

  • Recognition of known JSON Schema formats (UUIDs and IP addresses, for example)
  • Support for JSON or JSON Schema extensions.
Metadata

Version

0.1.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows