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

Related tags

JSON python json typing
Overview

JSONloader

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

  • builds a straightforward Python object from loaded JSON.
  • checks that your input-loaded-JSON has all necessary attributes for your pipeline.
  • checks that your input JSON has the right types.

Example

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

>> 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 >>> 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. >>> data = {'a': 'aa', 'b': 'bb'} >>> @JSONclass(annotations=True, annotations_strict=True, annotations_type=True) ... class Example: ... a : str ... b : int ... >>> try: ... example = Example(data) ... except TypeError: ... print("error - b is int") ... error - b is int">
>>> from jsonloader import JSONclass
>>> # By default we don't check for anything, we just build the object
>>> # as we received it.
>>> 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
>>> 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
>>> 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.
>>> data = {'a': 'aa', 'b': 'bb'}
>>> @JSONclass(annotations=True, annotations_strict=True, annotations_type=True)
... class Example:
...     a : str
...     b : int
...
>>> try:
...     example = Example(data)
... except TypeError:
...     print("error - b is int")
...
error - b is int

Install

User installation

python3 -m virtualenv venv
. venv/bin/activate
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/0.4.2.

python3 -m virtualenv venv
. venv/bin/activate
pip3 install -e '.[dev]'

Run Tests

# From this repository top directory
nose2 -t . --with-doctest

Tests coverage

For example, leverage coverage module: nose2 -t . -C --coverage-report html

You might also like...
A JSON utility library for Python featuring Django-style queries and mutations.

JSON Enhanced JSON Enhanced implements fast and pythonic queries and mutations for JSON objects. Installation You can install json-enhanced with pip:

Python script to extract news from RSS feeds and save it as json.

Python script to extract news from RSS feeds and save it as json.

Marshall python objects to and from JSON

Pymarshaler - Marshal and Unmarshal Python Objects Disclaimer This tool is in no way production ready About Pymarshaler allows you to marshal and unma

JSONManipulator is a Python package to retrieve, add, delete, change and store objects in JSON files.

JSONManipulator JSONManipulator is a Python package to retrieve, add, delete, change and store objects in JSON files. Installation Use the package man

cysimdjson - Very fast Python JSON parsing library

Fast JSON parsing library for Python, 7-12 times faster than standard Python JSON parser.

simplejson is a simple, fast, extensible JSON encoder/decoder for Python

simplejson simplejson is a simple, fast, complete, correct and extensible JSON http://json.org encoder and decoder for Python 3.3+ with legacy suppo

import json files directly in your python scripts
import json files directly in your python scripts

Install Install from git repository pip install git+https://github.com/zaghaghi/direct-json-import.git Use With the following json in a file named inf

Python script for converting .json to .md files using Mako templates.

Install Just install poetry and update script dependencies Usage Put your settings in settings.py and .json data (optionally, with attachments) in dat

Editor for json/standard python data
Editor for json/standard python data

Editor for json/standard python data

Ibmi-json-beautify - Beautify json string with python

Ibmi-json-beautify - Beautify json string with python

Jefferson Vaughn 3 Feb 2, 2022
Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

boris 5 Dec 1, 2021
Creates fake JSON files from a JSON schema

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Andy Challis 86 Jan 3, 2023
Random JSON Key:Pair Json Generator

Random JSON Key:Value Pair Generator This simple script take an engish dictionary of words and and makes random key value pairs. The dictionary has ap

Chris Edwards 1 Oct 14, 2021
With the help of json txt you can use your txt file as a json file in a very simple way

json txt With the help of json txt you can use your txt file as a json file in a very simple way Dependencies re filemod pip install filemod Installat

Kshitij 1 Dec 14, 2022
json|dict to python object

Pyonize convert json|dict to python object Setup pip install pyonize Examples from pyonize import pyonize

bilal alpaslan 45 Nov 25, 2022
Convert your JSON data to a valid Python object to allow accessing keys with the member access operator(.)

JSONObjectMapper Allows you to transform JSON data into an object whose members can be queried using the member access operator. Unlike json.dumps in

Owen Trump 4 Jul 20, 2022
Console to handle object storage using JSON serialization and deserealization.

Console to handle object storage using JSON serialization and deserealization. This is a team project to develop a Python3 console that emulates the AirBnb object management.

Ronald Alexander 3 Dec 3, 2022
A Cobalt Strike Scanner that retrieves detected Team Server beacons into a JSON object

melting-cobalt ?? A tool to hunt/mine for Cobalt Strike beacons and "reduce" their beacon configuration for later indexing. Hunts can either be expans

Splunk GitHub 150 Nov 23, 2022
jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

Denis Volk 3 Jan 9, 2022