Skip to main content

JSON Model

JSON Model is a compact and intuitive JSON syntax to describe JSON data structures.

This reference implementation allows to generate code in Python, C, JavaScript, PL/pgSQL, Perl and Java for checking a JSON value against a JSON model, and to export models to JSON Schema or Pydantic.

It is dedicated to the Public Domain.

Status Python Version Badges License

JMC Command

JSON Model optimizing compiler (jmc) can be installed as a Python package or a Container image docker.io/zx80/jmc, see Installation HOWTO.

Command jmc options include:

  • main operations (default depends on other options, final guess is preprocess):
    • -P: preprocess model.
    • -C: compile to Python, C, JS, PL/pgSQL, Perl, Java.
    • -E: export to JSON Schema version draft 2020-12 or Pydantic.
  • -O: optimize model: constant propagation, partial evaluation, xor to or conversion, flattening… (this is the default, -nO to disable)
  • -o output: file output instead of standard
  • …

For instance, let's consider a JSON model in file person.model.json:

{
  "#": "A person with a birth date",
  "name": "/^[a-z]+$/i",
  "born": "$DATE"
}
  • to check directly sample JSON values against it (with the Python backend):

    jmc -r person.model.json hobbes.json oops.json
    
    hobbes.json: PASS
    oops.json: FAIL (.: not an expected object [.]; .: missing mandatory prop <born> [.])
    
  • to compile an executable for checking a model (with the C backend), and use it for validating values:

    jmc -o ./person.out person.model.json
    ./person.out -r hobbes.json oops.json
    
    hobbes.json: PASS
    oops.json: FAIL (.: not an expected object [.]; .: missing mandatory prop <born> [.])
    

    The generated executable allow to collect validation performance figures (average and standard deviation) over a loop, with or without reporting:

    ./person.out -r -T 100000 hobbes.json
    
    hobbes.json.[0] nop PASS 0.056 ± 0.423 µs/check (0.174)
    hobbes.json.[0] rep PASS 0.071 ± 0.443 µs/check (0.174)
    hobbes.json: PASS
    
  • to export this model as a JSON schema in the YaML format:

    jmc -E -F yaml person.model.json
    
    description: A person with a birth date
    type: object
    properties:
      name:
        type: string
        pattern: (?i)^[a-z]+$
      born:
        type: string
        format: date
    required:
    - name
    - born
    additionalProperties: false
    

JSON Model Python API

The package provides functions to create and check models from Python:

import json_model as jm

# direct model definition with 2 mandatory properties
person_model: jm.Jsonable = {
  "name": "/^[a-z]+$/i",
  "born": "$DATE"
}

# create a dynamically compiled checker function for the model
checker = jm.model_checker_from_json(person_model)

# check valid data
good_person = { "name": "Hobbes", "born": "2020-07-29" }
print(good_person, "->", checker(good_person))

# check invalid data
bad_person = { "name": "Oops" }
print(bad_person, "->", checker(bad_person))

# collect reasons
reasons: jm.Report = []
assert not checker(bad_person, "", reasons)
print("reasons:", reasons)

JSON Model Validation Performance

See the benchmark page for artifacts which compare various JSON Model Compiler runs (C, JS, Java, Python) with Sourcemeta Blaze CLI as a baseline using test cases from JSON Schema Benchmark. Overall, JMC-C implementation is about twice faster than Blaze C++. Moreover, JMC-Java/GSON native implementations are only about 50% slower than Blaze C++, and JMC-JS 200% slower, which given the intrinsic language capabilities is quite honorable.

More Information

See the JSON Model website, which among many resources, includes a tutorial for a hands-on overview of JSON Model, and links to research papers for explanations about the design.

JSON Model Distribution

Metadata

Release files for json-model-compiler 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-model-compiler 2.1.0
File Size Uploaded Provenance Actions
json_model_compiler-2.1.0.tar.gz 282.5 kB Without provenance Details for json_model_compiler-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-model-compiler 2.1.0
File Interpreter ABI Platform Actions
json_model_compiler-2.1.0-py3-none-any.whl Python 3 none any Details for json_model_compiler-2.1.0-py3-none-any.whl

Total release size: 531.6 kB

Release files / json_model_compiler-2.1.0.tar.gz

Download URL json_model_compiler-2.1.0.tar.gz
Size 282.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ab4b63c57bfca6235bc490cf68714d1f94c51d981444a7c2ff43e04b47fb9b1f
BLAKE2b-256 checksum
How to use checksums
172824933a9998f64ac7cb959db23f9f6c6f8e03881418a38969d22336702272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / json_model_compiler-2.1.0-py3-none-any.whl

Download URL json_model_compiler-2.1.0-py3-none-any.whl
Size 249.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02ef8614f290bd579f9225b64107d19b50e48e91b69fadafeb3df89313806d92
BLAKE2b-256 checksum
How to use checksums
4567d2596b119516b01d14279b0c8fa9de24702900b50f3ee09f1f8c48b4ebd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for json-model-compiler 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page