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 https://pypip.in/d/json-spec/badge.png

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 cli commands like:

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.9.9.tar.gz (823.3 kB view details)

Uploaded Source

Built Distribution

json_spec-0.9.9-py2.py3-none-any.whl (45.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json-spec-0.9.9.tar.gz.

File metadata

  • Download URL: json-spec-0.9.9.tar.gz
  • Upload date:
  • Size: 823.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for json-spec-0.9.9.tar.gz
Algorithm Hash digest
SHA256 c9a403178d86ff58bea8233ef57278577b7da4ccdc1366ea3a3b698612cfc4b0
MD5 fe5e7d2dfbd2503b294e4e9f99e227ac
BLAKE2b-256 1f20f8cd399775838ed1680a53b3c6f99d5affc2688872e93cfe071f284ae2a6

See more details on using hashes here.

Provenance

File details

Details for the file json_spec-0.9.9-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for json_spec-0.9.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b0b5f96f2c9b286f2577e5b46c6f0428bee2b82aefe8ff4df6f2f6a952ddf81b
MD5 668a6abb7fdaa1b5b6fe4fb6f27ba1eb
BLAKE2b-256 9fe4d90adc2c30f84fb2298a277352536c32888cef3a3aa4caf15c4bfe1e268b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page