Skip to main content

JSON Schema Utils

Project description

JSON Schema Utils

Random utilities to analyze, manipulate, simplify, convert or compile JSON Schema.

Installation

Install latest version with pip from PyPI or directly from GitHub:

python -m venv venv
source venv/bin/activate
# from PyPI:
pip install json_schema_utils
# OR from latest sources:
pip install git+https://github.com/zx80/json-schema-utils.git

Inline Schema References

Replace references $ref by their definitions.

# no $id
jsu-inline -m "file:// ./tests" tests/*.schema.json
# with $id
jsu-inline -a tests/foo.schema.json

Simplify Schema

Apply various schema simplifications:

  • remove type-incompatible keywords and formats, with warnings.
  • try to move up list-of-one-schema *Of.
  • simplify type lists.
  • change list-of-one enum to const.
  • detect some cases of infeasible schemas.
  • remove uneffective keywords in corner cases.
jsu-simpler tests/*.schema.json

Check JSON Values against a Schema

Check a JSON values match a given schema using jmc (jsu-compile with jmc dynamic Python backend), jsonschema or jschon implementations.

jsu-check tests/foo.schema.json tests/foo.*.value.json

Note: the corresponding external dependencies must be installed.

JSON Schema Stats and Issues

Generate a report about JSON schemas, including possible bugs.

jsu-stats tests/*.schema.json

This script is extracted from JSON Schema Stats which has been updated to depend on this module.

JSON Prettyprinter

jsu-pretty --indent 2 --sort-keys tests/*.schema.json

You could also use jq . for this purpose.

JSON Schema to JSON Model Conversion

Convert a subset of JSON Schema to JSON Model. The subset should comply with some restrictions described in Section 6 of An Analysis of Defects in Public JSON Schemas by Claire Yannou-Medrala and Fabien Coelho:

  • const, enum, $ref, type, allOf, anyOf, oneOf should be exclusive.
  • some keywords are not supported: multipleOf, contains
  • conditions if then else are translated to the logical equivalent: {if: C, then: T, else: E} is (C and T) or (not C and E)
jsu-model test/foo.schema.json

JSON Schema Compiler

Convert the input schema and generate C, JS, Python, Java, Perl or even PL/pgSQL code using the JSON Model Compiler as a backend.

# generate a python script
jsu-compile -o some.py some.schema.json
# run the script to validate values
./some.py val1.json val2.json

The generated code (here the some.py Python script) needs some runtime which is installed with the compiler python module. Other languages require specific runtimes, which are available in the docker.io/zx80/jmc container image, or can be installed independently, see JSON Model HOWTO.

TODO

  • improve model conversion resilience by ignoring unexpected stuff
  • stats: warn instead of errors on unsure issues under if/then/else/not.
  • propagate non type under containers (*Of, if, then, else, reference?) to reduce false positive errors/warnings.
  • simplify: deduplicate constants in enum?
  • Testing. CI.

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_schema_utils-0.9.0.tar.gz (60.0 kB view details)

Uploaded Source

Built Distribution

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

json_schema_utils-0.9.0-py3-none-any.whl (59.7 kB view details)

Uploaded Python 3

File details

Details for the file json_schema_utils-0.9.0.tar.gz.

File metadata

  • Download URL: json_schema_utils-0.9.0.tar.gz
  • Upload date:
  • Size: 60.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for json_schema_utils-0.9.0.tar.gz
Algorithm Hash digest
SHA256 0eeea749eec11f894fac59d1e3e8a35ff557065b04dd4150b0cade0af2d25a68
MD5 232aed9486342a93c13af0cb3abc9ec4
BLAKE2b-256 18cc62d38103863183f4dfc535fc475f496b72f726d03edf507323223e6c3247

See more details on using hashes here.

File details

Details for the file json_schema_utils-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_schema_utils-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30209836834f649dcb2545e4c14302bc6a3d3e9552ae8535c30a86503092186b
MD5 dd60ad4d198a9c63fa8b0b32c8498f58
BLAKE2b-256 f3fef4ba5951f4e1d721a7624f0a227ba1e56ffc1afe3993934d782ffa8a6f5a

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