Skip to main content

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

Metadata

Release files for jsonloader 0.9.2

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

Source distribution (sdist)

Source distribution for jsonloader 0.9.2
File Size Uploaded Provenance Actions
jsonloader-0.9.2.tar.gz 8.5 kB Without provenance Details for jsonloader-0.9.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jsonloader 0.9.2
File Interpreter ABI Platform Actions
jsonloader-0.9.2-py3-none-any.whl Python 3 none any Details for jsonloader-0.9.2-py3-none-any.whl

Total release size: 15.6 kB

Release files / jsonloader-0.9.2.tar.gz

Download URL jsonloader-0.9.2.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
810203b6183330b2b9071930e10bf24e5bcc34dbd057c19510c944a8b3ce11ac
BLAKE2b-256 checksum
How to use checksums
c335f15753385c8b08aaebf2b0402ee6a4a8cdc85744b1df60b103eb05cb65c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / jsonloader-0.9.2-py3-none-any.whl

Download URL jsonloader-0.9.2-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95500662507926cccbe87deb843b3519de2f06566e2b7d417a8fec6b7da7f555
BLAKE2b-256 checksum
How to use checksums
c28875af2c044632c45bef3e8850daa9debda103cb9e4dbe73762bd1336642d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for jsonloader 0.9.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.2 This release

2 release files No provenance

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