Skip to main content

No more boilerplate to check and build a Python object from JSON.

Project description

JSONloader

Downloads CI

This module is for you if you're tired of writing boilerplate that:

  • builds a straightforward Python object from loaded JSON or similar dict-based data loading (e.g. CSV).
  • checks that your input-loaded-JSON has all necessary attributes for your pipeline.
  • checks that your input JSON has the right types.

Examples

Main intended usage is through the JSONclass decorator, example below:

By default we don't check for anything, we just build the object as we received it.

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONclass
... class Example:
...     pass
...
>>> example = Example(data)
>>> example.a
'aa'
>>> example.b
'bb'

We want to ensure we have annotated parameters

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONclass(annotations=True)
... class Example:
...     a: str
...     d: int
...
>>> try:
...     example = Example(data)
... except KeyError:
...     print("error - missing 'd'")
...
error - missing 'd'
>>> data['d'] = 1  # Let's fix the missing data
>>> example = Example(data)  # No more error in loading.

We want to ensure we have only annotated parameters

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa', 'b': 'bb', 'c': 1}
>>> @JSONclass(annotations=True, annotations_strict=True)
... class Example:
...     a: str
...     b: int
...
>>> try:
...     example = Example(data)
... except KeyError:
...     print("error - extra 'c'")
...
error - extra 'c'
>>> del data['c']  # Let's remove unwanted data
>>> example = Example(data)  # No more error in loading.

We want to ensure we have only annotated parameters and they are of annotated type.

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa', 'b': 'bb'}
>>> @JSONclass(annotations_strict=True, annotations_type=True)
... class Example:
...     a: str
...     b: int
...
>>> try:
...     example = Example(data)
... except TypeError:
...     print("error - b is not int")
...
error - b is not int

Default values are supported too.

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa'}
>>> @JSONclass(annotations_strict=True, annotations_type=True)
... class Example:
...     a: str
...     b: int = 1
...
>>> example = Example(data)
>>> example.b
1

A JSONclass can be converted back to a dict, even for recursive structures

>>> from jsonloader import JSONclass
>>> data = {'a': 'aa', 'b':2, 'c': {'foo': 'bar'}}
>>> @JSONclass(annotations_type=True, annotations=True)
... class Example:
...     a: str
...     b: int
...
>>> example = Example(data)
>>> assert dict(example) == data

Install

User installation

# Recommendation: install jsonloader in your project virtualenv
# Should you not want to use virtualenv or equivalent, it's recommended to use
# '--user' pip option to avoid a system-level install.
pip3 install jsonloader

Developer installation

Github repository currently points to latest development version. Please jump to latest released version tag if you intend to work on PyPI version. For example git checkout tags/v0.4.3.

# These commands assume virtualenv is installed
python3 -m virtualenv venv
. venv/bin/activate

# Actually install the deps
pip3 install -e '.[dev]'

# To setup the project's git hooks:
git config --local core.hooksPath hook

Run Tests

# From this repository top directory
pytest --doctest-modules

Tests coverage

For example, leverage coverage module:

coverage run -m pytest --doctest-modules
coverage html

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

jsonloader-0.8.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

jsonloader-0.8.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file jsonloader-0.8.1.tar.gz.

File metadata

  • Download URL: jsonloader-0.8.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for jsonloader-0.8.1.tar.gz
Algorithm Hash digest
SHA256 c676a0c4fabf7755d64b8e3923648a5176c23df001b613f71bda97f28e8a8b23
MD5 705a86df312b9bd57003c3deb787817b
BLAKE2b-256 25a63afddcb4366471aa3c06e950357d526c322445708d1765f18b6585192dac

See more details on using hashes here.

File details

Details for the file jsonloader-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: jsonloader-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for jsonloader-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 58a6525d32d7375eb8d7c3a0a183894ff7cb8d8d8b22268f2e1c95ad94320ddc
MD5 cb110fb618a24633385e52e24a8770a4
BLAKE2b-256 fccd7a9b0d1924bb3d0abd3e0410a74f79768391a19386c8e8b8be7181407675

See more details on using hashes here.

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