Skip to main content

Python dict-like object which abstracts resolution of JSONSchema references

Project description

Build Status

JSONSchema Ref Dict

Python dict-like object which abstracts resolution of JSONSchema references.

from json_ref_dict import RefDict

schema = RefDict("")

Nested items containing "$ref" will be resolved lazily when accessed, meaning the dictionary can be treating as a single, continuous (and possibly infinite) document.

Remote references are supported, and will be resolved relative to the current document.

If no scheme is provided, it is assumed that the document is present on the local filesystem (see Example below).

If PyYAML is installed, then loading of YAML documents will be supported, otherwise only JSON documents may be loaded.


Given the following related schemas:


    type: string
    $ref: '#/definitions/foo'
    $ref: 'other.yaml#/definitions/bar'
    $ref: 'other.yaml#/definitions/baz'


    type: integer
    $ref: 'master.yaml#/definitions/foo'

We can parse these as a single object as follows:

from json_ref_dict import RefDict

schema = RefDict("master.yaml#/definitions")
>>> {'foo': {'type': 'string'}, 'local_ref': {'$ref': '#/definitions/foo'}, 'remote_ref': {'$ref': 'other.yaml#/definitions/bar'}, 'backref': {'$ref': 'other.yaml#/definitions/baz'}}

>>> {'type': 'string'}

>>> {'type': 'integer'}

>>> {'type': 'string'}

Materializing documents

If you don't want the lazy behaviour, and want to get all of the IO out of the way up front, then you can "materialize" the dictionary:

from json_ref_dict import materialize, RefDict

schema = materialize(RefDict(""))
assert isinstance(schema, dict)

A materialized RefDict is just a regular dict, containing a document with all references resolved. This is useful if, for example, you want to cache/persist the entire schema. Be aware that if there are cyclical references in the schema, these will be present on the materialized dictionary.

The materialize helper also supports some basic transformation options, as performing global transformations on infinite documents is non-trivial:

  • include_keys - an iterable of keys to include in the materialized document.
  • exclude_keys - an iterable of keys to exclude from the materialized document.
  • value_map - an operation to apply to the values of the document (not lists or dictionaries).


This package is currently tested for Python 3.6.


This project may be installed using pip:

pip install json-ref-dict


  1. Clone the repository: git clone && cd json-ref-dict
  2. Install the requirements: pip install -r requirements.txt -r requirements-test.txt
  3. Run pre-commit install
  4. Run the tests: bash -c -a

This project uses the following QA tools:

  • PyTest - for running unit tests.
  • PyLint - for enforcing code style.
  • MyPy - for static type checking.
  • Travis CI - for continuous integration.
  • Black - for uniform code formatting.


This project is distributed under the MIT license.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for json-ref-dict, version 0.6.1
Filename, size File type Python version Upload date Hashes
Filename, size json_ref_dict-0.6.1-py3-none-any.whl (11.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size json-ref-dict-0.6.1.tar.gz (10.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page