Skip to main content

Handle JSON objects more simply, like you do in JavaScript.

This Python module provides an interface to JSON data structures that is more reminiscent of the compact and forgiving syntax that you use in JavaScript than the verbose and picky Python 'dict' interface.

For example, instead of:

from pathlib import Path
import json

with Path('data.json').open(encoding='utf-8') as fh:
    data = json.load(fh)
version = (data.get('summary') or {}).get('version')

you can write:

from pathlib import Path
from opchjson import JSON

data = JSON.parse(Path('data.json'))
version = data.summary.version

i.e. you can access object properties using the . operator, just like in JavaScript. If you access a non-existent property, it will return JSON.undefined. Note that one difference from JavaScript behaviour is that if you try and retrieve properties from undefined it will return undefined rather than throwing an exception.

Note that you cannot access object properties that start with an underscore character via the . operator, as this is reserved for accessing Python dictionary methods - e.g. data._items(). If you want to access a property that begins with _, simply use the standard Python [] operator- e.g. data['_foo']. This will similarly return JSON.undefined if the property does not exist.

Usage

To use the module, do from opchjson import JSON. If you wish, you can also import undefined, to save a bit of typing on JSON.undefined.

JSON()

If you already have a data structure in memory that is suitable for JSON representation, e.g. a Python dict, you can pass it to JSON() to convert it into opchjson representation.

JSON.parse()

JSON.parse takes a single positional argument, which can be a string, bytes, a bytearray, a pathlib Path, or any file-like object. If it is a Path, the file will be assumed to be UTF-8 encoded unless you provide an encoding keyword argument specifying a different encoding. You can also pass any keyword argument that the Python standard library json.load function would accept.

The returned value will be None, a bool, an int, a float, a string, or an instance of JSON.Array or JSON.Object.

JSON.stringify()

This function is similar to the JavaScript one of the same name. The first argument is the value to encode as JSON. The remaining arguments are either positional arguments as per JavaScript, or keyword arguments as per Python's json.dumps, or a mixture of both.

JSON.Array

Instances of this class are essentially identical to Python lists, except that they will return JSON.undefined rather than throwing exceptions when you try to retrieve list elements that don't exist. Note that empty Arrays count as falsey, like Python and unlike JavaScript.

JSON.Object

Instances of this class are essentially identical to Python dicts, except that they will return JSON.undefined rather than throwing exceptions when you try to retrieve keys that don't exist, and attribute references will be looked up in the dictionary unless the attribute name starts with an underscore. So, for example, obj.foo is identical to dict.get('foo', JSON.undefined). Note that empty Objects count as falsey, like Python and unlike JavaScript.

Objects also have an _ attribute, which is set to an empty types.SimpleNamespace. It can be used to store ancillary data that will not appear in the output JSON representation.

JSON.undefined

This is a singleton instance that behaves like an empty, immutable JSON.Object. Test for it using foo is JSON.undefined.

History

0.2.0 (2026-06-05)

  • Add Object._ SimpleNamespace attribute.

0.1.0 (2026-06-02)

  • Initial version.

Metadata

Release files for opchjson 0.2.0

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

Source distribution (sdist)

Source distribution for opchjson 0.2.0
File Size Uploaded Provenance Actions
opchjson-0.2.0.tar.gz 21.0 kB Without provenance Details for opchjson-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opchjson 0.2.0
File Interpreter ABI Platform Actions
opchjson-0.2.0-py3-none-any.whl Python 3 none any Details for opchjson-0.2.0-py3-none-any.whl

Total release size: 42.3 kB

Release files / opchjson-0.2.0.tar.gz

Download URL opchjson-0.2.0.tar.gz
Size 21.0 kB
Tags Source
SHA-256 checksum
How to use checksums
06f7a8f07ddbcc6fe2f65316bb8b06045fb9b70386a81801990fc27ede788ca9
BLAKE2b-256 checksum
How to use checksums
ca4b12ccbdaa448a574429535f71e1add761fcd1c7e78877dd85aa704fcbe8b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / opchjson-0.2.0-py3-none-any.whl

Download URL opchjson-0.2.0-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58a14b90c5cf45bab958573539c780f170153efbaed99dd5db3bef718cca9148
BLAKE2b-256 checksum
How to use checksums
80520352c3f5a18fdde25634fd106eeaa7e5dac733c21dcc06955936a92dd858
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for opchjson 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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