Implements JSON Schema, JSON Pointer and JSON Reference.
Project description
This library implements several JSON specs, like JSON Schema, JSON Reference and JSON Pointer:
It works on python 2.7, python 3.3 and above
It is release under the BSD license
Installation
This library has only weak dependencies. You can simply use pip:
$ pip install json-spec
Regading you needs, you can install more features. For example, this command will enable colorated messages:
$ pip install json-spec[cli]
This one will enable ip format for json schema:
$ pip install json-spec[ip]
…
CLI Usage
This module expose 2 cli commands.
json-extract will extract parts of your json document:
$ json-extract '#/foo/1' --document-json='{"foo": ["bar", "baz"]}' $ echo '{"foo": ["bar", "baz"]}' | json-extract '#/foo/1' $ json-extract '#/foo/1' --document-file=doc.json $ json-extract '#/foo/1' < doc.json
json-validate will validate your document against a schema:
$ json-validate --schema-file=schema.json --document-json='{"foo": ["bar", "baz"]}' $ echo '{"foo": ["bar", "baz"]}' | json-validate --schema-file=schema.json $ json-validate --schema-file=schema.json --document-file=doc.json $ json-validate --schema-file=schema.json < doc.json
Library usage
Let say you want to fetch / validate JSON like objects in you python scripts.
You can extract member of an object with JSON Pointer:
from jsonspec.pointer import extract obj = { 'foo': ['bar', 'baz', 'quux'] } assert 'baz' == extract(obj, '/foo/1')
You can resolve member of any object with JSON Reference:
from jsonspec.reference import resolve obj = { 'foo': ['bar', 'baz', { '$ref': '#/sub' }], 'sub': 'quux' } assert 'quux' == resolve(obj, '#/foo/2')
You can describe you data with JSON Schema:
from jsonspec.validators import load # data will validate against this schema validator = load({ 'title': 'Example Schema', 'type': 'object', 'properties': { 'age': { 'description': 'Age in years', 'minimum': 0, 'type': 'integer' }, 'firstName': { 'type': 'string' }, 'lastName': { 'type': 'string' } }, 'required': [ 'firstName', 'lastName' ] }) # validate this data validator.validate({ 'firstName': 'John', 'lastName': 'Noone', 'age': 33, })
Other examples can be found in the documentation or in the tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file json-spec-0.11.0a1.tar.gz
.
File metadata
- Download URL: json-spec-0.11.0a1.tar.gz
- Upload date:
- Size: 30.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.8 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ac837123bbd1a591aacc592b76cd9f149c4fff3b7c89c224fca9569bc185e12 |
|
MD5 | d15228206c527e350dd0e09f54958972 |
|
BLAKE2b-256 | 988029ae8348b82a5a1b83e9eefbcb3a1e3848f3e3b1c5847f259ea43c4b92d2 |
Provenance
File details
Details for the file json_spec-0.11.0a1-py3-none-any.whl
.
File metadata
- Download URL: json_spec-0.11.0a1-py3-none-any.whl
- Upload date:
- Size: 41.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.8 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 885dfb074e1af529c959992dce9df5675abb54006ff78a992308f95d43ea66df |
|
MD5 | e565c853254bdfef0a2fcb5526845deb |
|
BLAKE2b-256 | 8e460536a6c3393b013b336fee472569204300b0cc038c59917b5294ee8a17cd |