Skip to main content

Json schema Model

Project description

Build status Downloads Latest Version License

Build python objects using JSON schemas:

>>> import jsonschema_model
>>> Model = jsonschema_model.model_factory({
...    "name": "Model",
...    "properties": {
...        "foo": {"type": "string"},
...        "bar": {"type": "array", "items": {
...            "type": "object",
...            "name": "Bar",
...            "properties": {
...                "zaz": {"type": "string"},
...            },
...        }},
...    }})

# Simple object creation
>>> obj = Model(foo="bar")
>>> assert obj == {"foo": "bar"}

# Nested and array are implemented
# HINT: Use add() instead of append()
>>> obj.bar.add(zaz="qux")
{'zaz': 'qux'}
>>> assert obj == {'foo': 'bar', 'bar': [{'zaz': 'qux'}]}

# You can access via attribute or via dict like interface
>>> obj["bar"][0].zaz
'qux'

# Array have a special get_or_create() method
# to avoid dupplicates within an array
>>> obj.bar.get_or_create(zaz="xuq")
{'zaz': 'xuq'}
>>> obj.bar
[{'zaz': 'qux'}, {'zaz': 'xuq'}]
>>> obj.bar.get_or_create(zaz="xuq")
{'zaz': 'xuq'}
>>> obj.bar
[{'zaz': 'qux'}, {'zaz': 'xuq'}]

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

jsonschema-model-1.4.0.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

jsonschema_model-1.4.0-py2.py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 2 Python 3

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