Skip to main content

Standalone JsonObject helper utilities

Project description

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.8
  • 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

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

kingkybel_pyjsonobject-0.2.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

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

kingkybel_pyjsonobject-0.2.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kingkybel_pyjsonobject-0.2.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for kingkybel_pyjsonobject-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b98b611db6f1daf53b86e3f3378c0f1eab743d04c415f51893b2136649bd48ee
MD5 db87d923b780d3528d7814dcfe375041
BLAKE2b-256 170ab51e2db9f4df0eb18e366a408516c2ba4a9ffd67cf2e5032ee7a34bf8c57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kingkybel_pyjsonobject-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1037c7e6b9bed6e4b7ba46041687175ea149ff98c253d3d0e2197964cb22c9c7
MD5 43449d17522178df2590f1ea880138e1
BLAKE2b-256 71aa9d465196e9b97cfe359baaa8ee18e79656e397dbf4d14a4947b19086a717

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