Skip to main content

python-jref

This python package implements the JSON Reference and JSON Pointer specifications in the context of processing JSON and YAML content, such as Swagger and OpenAPI specifications.

JSON Reference

JSON Reference defines a mechanism to reference and include content from the same or separate documents. This helps with maintenance of structured data and documents by reducing repetition, and allowing for the organization of larger documents across multiple files.

JSON Pointer

JSON Pointer defines a syntax for identifying specific portions of a JSON value.

In a document, a JSON Reference takes the form of an object, mapping '$ref' to a reference; other keys in the object are ignored. The reference itself takes the form of a URI. As an example:

{ "$ref": "http://example.com/example.json#/foo/bar" }

In this implementation, the URI may reference a JSON or YAML file available in the local filesystem, or served from the network over HTTP/HTTPS. The fragment portion of the URI is interpreted as a JSON Pointer.

Usage example

Find the example below also as usage-example.py in the source repository.

import os
import textwrap

import jref.context
import jref.pointer

# All references are evaluated in a context, so start with one
ctx = jref.context.RemoteContext()

# Reference a remote document
spec = ctx.parse_reference('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml')

# Reference portions of that document
spec_title = spec.context.parse_reference('#/info/title')
spec_version = spec.context.parse_reference('#/info/version')

# Print out the references, not the content (which hasn't been loaded)
print(textwrap.dedent('''
    * JSON References
    Spec:    {}
    Title:   {}
    Version: {}
    ''')
    .lstrip()
    .format(spec, spec_title, spec_version))

# Expand references, triggering loading of content
print(textwrap.dedent('''
    * Evaluated references
    Title:   {}
    Version: {}
    ''')
    .lstrip()
    .format(spec_title.expand(), spec_version.expand()))

Expected output:

* JSON References
Spec:    https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml
Title:   https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml#/info/title
Version: https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml#/info/version

* Evaluated references
Title:   Swagger Petstore
Version: 1.0.0

References

Metadata

Release files for jref 0.4.1

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

Source distribution (sdist)

Source distribution for jref 0.4.1
File Size Uploaded
jref-0.4.1.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jref 0.4.1
File Interpreter ABI Platform
jref-0.4.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 41.9 kB

Release files / jref-0.4.1.tar.gz

Download URL jref-0.4.1.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ea69859bdaf768feb34b1eb46ae52aefc90a4b436a6602ce7a106376373be3e8
BLAKE2b-256 checksum
How to use checksums
baea3a973f03432453b40f22356be39ae983156ea73cf637629b93fac605f555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / jref-0.4.1-py2.py3-none-any.whl

Download URL jref-0.4.1-py2.py3-none-any.whl
Size 23.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
272db831f52bc461de5b32a1c9d69d9fa9bd2b6de437174c57beea8178ad4215
BLAKE2b-256 checksum
How to use checksums
7bf3b2c5092d06f48c5eb9c509a6d9d173ebca60905136e3ef723c1bb4109e01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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