Skip to main content

Handle JSON files easily, like in JavaScript

Project description

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.

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

opchjson-0.2.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opchjson-0.2.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file opchjson-0.2.0.tar.gz.

File metadata

  • Download URL: opchjson-0.2.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for opchjson-0.2.0.tar.gz
Algorithm Hash digest
SHA256 06f7a8f07ddbcc6fe2f65316bb8b06045fb9b70386a81801990fc27ede788ca9
MD5 93f8afcc92d60ee2e9787a9986634d64
BLAKE2b-256 ca4b12ccbdaa448a574429535f71e1add761fcd1c7e78877dd85aa704fcbe8b5

See more details on using hashes here.

File details

Details for the file opchjson-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: opchjson-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for opchjson-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58a14b90c5cf45bab958573539c780f170153efbaed99dd5db3bef718cca9148
MD5 b6a8930846cc150db72af38651c4fb05
BLAKE2b-256 80520352c3f5a18fdde25634fd106eeaa7e5dac733c21dcc06955936a92dd858

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page