Skip to main content

Implements JSON Schema, JSON Pointer and JSON Reference.

Project description

https://badge.fury.io/py/json-spec.png https://travis-ci.org/johnnoone/json-spec.png?branch=master

This library implements several JSON specs, like JSON Schema, JSON Reference and JSON Pointer:

  • It works on python 3.6 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_spec-0.11.0.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

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

json_spec-0.11.0-py3-none-any.whl (41.3 kB view details)

Uploaded Python 3

File details

Details for the file json_spec-0.11.0.tar.gz.

File metadata

  • Download URL: json_spec-0.11.0.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

File hashes

Hashes for json_spec-0.11.0.tar.gz
Algorithm Hash digest
SHA256 c767d6599dee08cd8fa9cce40daad42ef09e469c864acb29a04997de7f8e9d3b
MD5 583d46a59fdf0cc37b2dba68b12fbb7b
BLAKE2b-256 3424e5a3dc99528f1984fac691fd229ab18eba15af475221fc495a5d20766d2b

See more details on using hashes here.

File details

Details for the file json_spec-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: json_spec-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 41.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/21.6.0

File hashes

Hashes for json_spec-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ce3eab5821c5c99fdd28782ea17bbeea0fec9fed98cf9c3aae72c154a50c055
MD5 ba112e321171c35e0e3efcc569b58250
BLAKE2b-256 991f8c8fb50311465876e4745e51699fdfde069f6aef0924a6332c075a07d40e

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