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 converter is expected to fully supports JSON Schema draft3 to draft7. Note that our tools include a significant support for format assertions, with 80-95% of optional format tests being validated, usually in a safe way (i.e. false positives may occur, false negatives are avoided).

It partially supports draft 2019-09 and 2020-12. In particular, vocabularies are fully supported. However, there is a limited support for some features:

  • dynamic anchors and references
  • unevaluated items and properties

Moreover, this is a software, hence there may be bugs.

jsu-model --format -o foo.model.json foo.schema.json

JSON Schema Compiler

Convert the input schema and generate C, JS, Python, Java, Perl or even PL/pgSQL code.

The compiler first converts the schema to a model (see previous command), then proceeds to compile the generated model to actual 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

  • propagate non type under containers (*Of, if, then, else, reference?) to reduce false positive errors/warnings.
  • simplify: deduplicate constants in enum?

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.10.tar.gz (69.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_schema_utils-0.9.10-py3-none-any.whl (73.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_schema_utils-0.9.10.tar.gz
  • Upload date:
  • Size: 69.6 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.10.tar.gz
Algorithm Hash digest
SHA256 8debb17a3f6ce57fb4f811ed703e188b3dddce566ccb1b668bc680be879978d2
MD5 537644000836dd9ab21dbf6ef0711ee8
BLAKE2b-256 96ad1c9edc8a55100d20f2234c6e4c2500ffae52d3e43224c18a64d9d86a6eaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for json_schema_utils-0.9.10-py3-none-any.whl
Algorithm Hash digest
SHA256 bc19864df3f4c82fbcb0de39e9461a527eac2a0102c876bee28d2eae9f9c7cd4
MD5 050b03b463fe47f82e06e14d0576b454
BLAKE2b-256 92648d80eb43db3bd56b2c9ade51d57d4dc20c6d4206d14f843427b7398ad5e3

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