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 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.9.14.tar.gz (83.8 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.9.14-py2.py3-none-any.whl (35.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for json-spec-0.9.14.tar.gz
Algorithm Hash digest
SHA256 7c54fc7ff5f639f56f8b612732406ba129f49b5207bb4840134a1a261126ace9
MD5 5032e88dfb318caf7afc63deb2d3140b
BLAKE2b-256 645b30ff03904cacc5800930787f9f4276431571c3c0763004fdcfe54772594a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for json_spec-0.9.14-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0949c84bdb756e7bfa44c8f68624116d98736654a5c4db94cb7c0824ebfab5d2
MD5 498b0641f58094ffe2106d426b2ae3f9
BLAKE2b-256 3c17e5e96940a83b692df4721a2249950acbbef9ca8a2d97c03dff45f383624a

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