Skip to main content

A JSON/YAML loader

Project description

Gitpod ready-to-code Coverage Status Pearmaster

jacobs-json-doc

A JSON/YAML loader for Python3.

PyYAML or ruamel.yaml do a good job at parsing YAML or JSON into a Python object. This package wraps ruamel.yaml to provide a little bit of extra functionality.

Line Numbers

If you're trying to do use parts of a JSON/YAML document, and you find that the structure of the document didn't conform to a schema or expectations, then you might want to display an error saying something like "On line 123, the value of foo was missing." This package allows easier access to the YAML/JSON line numbers by accessing the .line property.

Dollar References

JSON Schema, OpenAPI, AsyncAPI, and others have a concept of references like this: {"$ref": "other.json#/path/to/definition"}.

The idea here is that instead of the JSON object with the $ref you should be able to get a JSON structure from somewhere else. In this example, you should find a document called other.json and pull out a structure at /path/to/definition.

Loader

A loader object (inherits from jacobsjsondoc.loader.LoaderBaseClass) is able to get the JSON/YAML source. The loader can be different if you are loading from a database, filesystem, http, etc.

Reference Modes

Given a loader, jacobs-json-doc can deal with dollar references. There are two modes for how it can deal with references:

  • Use DocReference objects. Anywhere in the document tree where there is a $ref reference, a DocReference object is created.
  • Automatic resolution. Anywhere in the document tree where there is a $ref reference, the reference is automatically resolved and the $refs are replaced with the structures that they were referencing.

Examples

The test_document.py file is a good source for examples.

A very brief example is:

from jacobsjsondoc.document import create_document

my_document = "/path/to/example.yaml"
doc = create_document(uri=my_document)
print(doc['a']['b'])
print(doc['a']['b'].line)

If you are loading a single bit of data, without any $ref references, you can do it like this:

import jacobsjsondoc
text_data = '{"hello":"world"}'
doc = jacobsjsondoc.parse(text_data)

License

GPLv2

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

jacobs-json-doc-0.16.3.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

jacobs_json_doc-0.16.3-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file jacobs-json-doc-0.16.3.tar.gz.

File metadata

  • Download URL: jacobs-json-doc-0.16.3.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for jacobs-json-doc-0.16.3.tar.gz
Algorithm Hash digest
SHA256 d948feca007d07ecd5b11296bc46b87af4bfd580ab586e2b1bcf24906423365c
MD5 ac2685ecf67682fdf7a3a5563cd369ad
BLAKE2b-256 ebbbba370a013a4e6685cce46658eae7e2951d115ce56091d018000bf5187460

See more details on using hashes here.

File details

Details for the file jacobs_json_doc-0.16.3-py3-none-any.whl.

File metadata

File hashes

Hashes for jacobs_json_doc-0.16.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e62d02c818d21d1168fb8ac8d7ce48ec83d011d9aa215ea9b15b57ee987c2d3a
MD5 4239f24f2919f824b2ae988a5f7cdd58
BLAKE2b-256 e40e805344f4bd68259e191453d144b107dbf37652e0770d54089bf17b99ac52

See more details on using hashes here.

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