Skip to main content

A package for validating json according to a json schema, and type parsing it to python types.

Project description

pyjschema

Unlike other packages that handle the Json-Schema format, this package is pretending to give a solution for getting common pythonic types (like datetime, UUID, etc) from json data according to the Json-Schema format.

Alongside that, the package has validation capabilities like several other packages.

Features

  • Json schema validation
  • Json parsing according to a json schema
  • Extending Json-Schema by adding custom formats

Installation

$ pip install pyjschema

Examples

Basic Use

>>> from pyjschema import loads
>>> json = '{"uuid": "9449771f-56ca-44c7-b9f6-d20315a2c6e0"}'
>>> # Setting up a property "uuid" to be of format "uuid"
>>> schema = {
... 'type': 'object', 
... 'properties': {'uuid': {'type': 'string', 'format': 'uuid'}}
... }
>>> loads(json, schema)
{'uuid': UUID('3e4666bf-d5e5-4aa7-b8ce-cefe41c7568a')}

Custom Formats

The package allows also to extend the default Json-Schema formats and define more formats that will be structured in the json data. An example for that could be adding a "bytes" format that will be structured in the json as base64 phrase. An optional implementation for that with pyjschema would be:

from base64 import b64encode, b64decode
from pyjschema import Formatter, JsonSchemaParser

class BytesFormatter(Formatter):
    symbol = 'bytes'
    
    def decode(self, raw: str) -> bytes:
        return b64decode(raw)
    
    def encode(self, data: bytes) -> str:
        return b64encode(data).decode()


if __name__ == '__main__':
    schema = {'type': 'string', 'format': 'bytes'}
    parser = JsonSchemaParser(schema, extended_formats=[BytesFormatter])

    data = parser.loads('"SGVsbG8gV29ybGQh"')

    print(type(data), data)
    # output: <class 'bytes'> b'Hello World!'

References

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

pyjschema-1.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

pyjschema-1.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pyjschema-1.1.0.tar.gz.

File metadata

  • Download URL: pyjschema-1.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for pyjschema-1.1.0.tar.gz
Algorithm Hash digest
SHA256 659547abc46d7e8d278fb10ba4c0456ce153a37b9e938493044c2a2d18170a3c
MD5 beddd621f4054b8a74c266c87b15b00c
BLAKE2b-256 d712c5e4cb417b5dd077458a70dbe9ee170279a6242bb6bd55db8a48c0be0cb9

See more details on using hashes here.

File details

Details for the file pyjschema-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyjschema-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for pyjschema-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60101de3c1321d352cab63389cccf244d0f013ebedb9330d7ba45aa25826da34
MD5 32d16b9d0420da37b23f085752231b93
BLAKE2b-256 4150af20cc8a71bd44697fd5db3fc9181d4273394cfc96388aa3a2a17951ce3e

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