Skip to main content

JSON Model Tools

Project description

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.

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 faster than Blaze C++. Moreover, JMC-JS and JMC-Java/GSON native implementations are only about 50% slower than Blaze C++, 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

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_model_compiler-2.0b48.tar.gz (246.6 kB view details)

Uploaded Source

Built Distribution

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

json_model_compiler-2.0b48-py3-none-any.whl (225.6 kB view details)

Uploaded Python 3

File details

Details for the file json_model_compiler-2.0b48.tar.gz.

File metadata

  • Download URL: json_model_compiler-2.0b48.tar.gz
  • Upload date:
  • Size: 246.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for json_model_compiler-2.0b48.tar.gz
Algorithm Hash digest
SHA256 203ffc31e1baf440e742832932518b5d90ab04dcea121a83aa0a994a3bb34ead
MD5 f0460cddf53131cfa16e62f8776bd8c6
BLAKE2b-256 5023e071590a2beebc541cc1dfbef8e62dba13841cafed1c5618351d3833abbe

See more details on using hashes here.

File details

Details for the file json_model_compiler-2.0b48-py3-none-any.whl.

File metadata

File hashes

Hashes for json_model_compiler-2.0b48-py3-none-any.whl
Algorithm Hash digest
SHA256 34d1bb003e0304f7386ac9b2878964d256483398680b18265b14b413399b3c50
MD5 129bd0d169660de341755b90908b6e6f
BLAKE2b-256 63d5f7a1976220729a02d0e836f896d908a05f96150a74ecb73c15a0bb0758fe

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