Skip to main content
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 a cli command with multiple operations.

json add will transform the json document:

$ json add '#/foo/1' --fragment-file=fragment.json --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json add '#/foo/1' --fragment-json='first'
$ json add '#/foo/1' --fragment-file=fragment.json --document-file=doc.json
$ json add '#/foo/1' --fragment-file=fragment.json < doc.json

json check will test that a value at the target location is equal to a specified value:

$ json check '#/foo/1' --fragment-file=fragment.json --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json check '#/foo/1' --fragment-file=fragment.json
$ json check '#/foo/1' --fragment-file=fragment.json --document-file=doc.json
$ json check '#/foo/1' --fragment-file=fragment.json < doc.json

json copy will copy the value at a specified location to the target location:

$ json copy '#/foo/1' --target='#/foo/2' --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json copy '#/foo/1' --target='#/foo/2'
$ json copy '#/foo/1' --target='#/foo/2' --document-file=doc.json
$ json copy '#/foo/1' --target='#/foo/2' < doc.json

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 move will remove the value at a specified location and it will add the value to the target location:

$ json move '#/foo/2' --target='#/foo/1' --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json move '#/foo/2' --target='#/foo/1'
$ json move '#/foo/2' --target='#/foo/1' --document-file=doc.json
$ json move '#/foo/2' --target='#/foo/1' < doc.json

json remove will remove the value at a specified location:

$ json remove '#/foo/1' --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json remove '#/foo/1'
$ json remove '#/foo/1' --document-file=doc.json
$ json remove '#/foo/1' < doc.json

json replace will replace the value at a specified location with given fragment:

$ json replace '#/foo/1' --fragment-file=fragment.json --document-json='{"foo": ["bar", "baz"]}'
$ echo '{"foo": ["bar", "baz"]}' | json replace '#/foo/1' --fragment-file=fragment.json
$ json replace '#/foo/1' --fragment-file=fragment.json --document-file=doc.json
$ json replace '#/foo/1' --fragment-file=fragment.json < 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.

Metadata

Release files for json-spec 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-spec 0.12.0
File Size Uploaded
json_spec-0.12.0.tar.gz 29.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-spec 0.12.0
File Interpreter ABI Platform
json_spec-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 71.8 kB

Release files / json_spec-0.12.0.tar.gz

Download URL json_spec-0.12.0.tar.gz
Size 29.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0a1fe48bf3e6bce39668b4bacb99e479429138d77b16ab25c482b3238d4702e0
BLAKE2b-256 checksum
How to use checksums
4d4784e030fb7764bd716d2fb58d4bd45c006b407cd413f472a60f1e09265ceb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

Release files / json_spec-0.12.0-py3-none-any.whl

Download URL json_spec-0.12.0-py3-none-any.whl
Size 42.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5096695f1ed4d2bf70b04bee792615cc36f8f830f5a5a2255fc43731e4deba1e
BLAKE2b-256 checksum
How to use checksums
c07c31574832b5269f7a9385cec3b9b19d0a535f5ace075446ca9cbb240868bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page