Skip to main content

PyJsonObject banner

PyJsonObject

PyJsonObject is a Python library for reading and updating nested JSON values via slash-separated key paths.

Features

  • Read nested values with paths like user/profile/name
  • Read/write array values with bracket syntax, e.g. items/[0]/id
  • Special array tokens:
    • [^] = first element
    • [$] = last element
  • Safe reads with default=...
  • Optional force=True writes that create/reshape intermediate structures
  • File helpers: from_file(...), to_file(...)

Installation

Install from source (editable)

python3 -m pip install -e .

Build distribution artifacts

python3 -m pip install build
python3 -m build

Requirements

  • Python >=3.12
  • Runtime dependencies:
    • kingkybel-pyflashlogger>=2.5.0
    • kingkybel-pyfundamentals>=0.4.6

Path syntax

  • Path separator: /
  • Object key segment: config/theme
  • Array index segment: users/[0]/name
  • Supported array selectors: [0], [1], ... [^], [$]

Quick usage

Basic get/set

from pyjsonobject import JsonObject

obj = JsonObject(json_str='{"user": {"name": "Ada", "age": 36}}')
obj.set("user/name", "Ada Lovelace", force=False)
print(obj.get("user/name"))

Read with default

from pyjsonobject import JsonObject

obj = JsonObject(json_obj={"user": {"name": "Ada"}})
city = obj.get("user/city", default="unknown")
print(city)  # unknown

Force-create nested path

from pyjsonobject import JsonObject

obj = JsonObject(json_str="{}")
obj.set("a/[0]/name", "node-0", force=True)
print(obj.get("a/[0]/name"))

Array prepend/append tokens

from pyjsonobject import JsonObject

arr = JsonObject(json_str="[]")
arr.set("[$]", "last", force=True)   # append
arr.set("[^]", "first", force=True)  # prepend

print(arr.get("[^]"))  # first
print(arr.get("[$]"))  # last

Work with files

from pyjsonobject import JsonObject

obj = JsonObject(filename="input.json")
obj.set("meta/version", 2, force=True)
obj.to_file("output.json", indent=2)

API overview

  • JsonObject(json_str=None, filename=None, json_obj=None)
  • get(keys, default=None)
  • set(keys, value, force=False, dryrun=False)
  • update(keys, mapping, force=False, deep=False, dryrun=False)
  • delete(keys, silent=False, dryrun=False)
  • key_exists(keys)
  • get_many(paths, default=None)
  • from_string(json_str) / from_object(obj) / from_file(filename)
  • to_str(indent=2) / to_file(filename, indent=4, dryrun=False)
  • get_json() / to_dict() / copy()
  • JsonObject.assert_json_files_valid(paths)

Behavior notes

force=False (default)

  • The path must already exist and container types must match.
  • Leaf updates only succeed when the existing value type matches the new value type.
  • Type mismatches raise JsonValueMismatch.

force=True

  • Missing path segments are created as needed.
  • Existing incompatible intermediate containers may be reshaped to satisfy the target path.
  • [^] and [$] can be used to prepend/append when writing to arrays.

update(...) merge behavior

  • update(..., deep=False) performs a shallow dict update at the target path.
  • update(..., deep=True) recursively merges nested dictionaries.
  • force=True allows creation/replacement of the target path with a dict before merge.

Public typing aliases

The implementation provides explicit aliases in pyjsonobject.types:

  • JSONScalar = bool | int | float | str
  • JSONValue = JSONScalar | list | dict
  • JSONObjectContainer = list | dict

Development

Run tests:

python3 -m pytest -q

Metadata

Release files for kingkybel-pyjsonobject 0.2.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 kingkybel-pyjsonobject 0.2.1
File Size Uploaded Provenance Actions
kingkybel_pyjsonobject-0.2.1.tar.gz 27.1 kB Without provenance Details for kingkybel_pyjsonobject-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kingkybel-pyjsonobject 0.2.1
File Interpreter ABI Platform Actions
kingkybel_pyjsonobject-0.2.1-py3-none-any.whl Python 3 none any Details for kingkybel_pyjsonobject-0.2.1-py3-none-any.whl

Total release size: 51.4 kB

Release files / kingkybel_pyjsonobject-0.2.1.tar.gz

Download URL kingkybel_pyjsonobject-0.2.1.tar.gz
Size 27.1 kB
Tags Source
SHA-256 checksum
How to use checksums
41355e33b908574a2a36a4bf68bde36cfac790d563c3c77c74ac98f25c3ecf85
BLAKE2b-256 checksum
How to use checksums
6da68c46790940a3d5643827a971e05fb5c1f0c2087c4b61b3c45fc73235a7be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / kingkybel_pyjsonobject-0.2.1-py3-none-any.whl

Download URL kingkybel_pyjsonobject-0.2.1-py3-none-any.whl
Size 24.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64acc84b8f6f0e7c47b6febd0b731121401eb4ce2dec8cf2404d29cb07285191
BLAKE2b-256 checksum
How to use checksums
fa6a99d9bac8135698f0e883656345e05cb366feb7ce21afc88ef0330f8402ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for kingkybel-pyjsonobject 0.2.1

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.

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