Skip to main content

Standalone JsonObject helper utilities

Project description

PyJsonObject banner

PyJsonObject

PyJsonObject is a Python library for reading and modifying nested JSON values using slash-separated key paths. It is the Python counterpart of the C++ JsonObject library and follows the same core usage model.

What this repository contains

  • Core package:
    • pyjsonobject/json_object.py
    • pyjsonobject/json_key_path.py
    • pyjsonobject/exceptions.py
  • Unit tests in test/
  • Packaging and build metadata:
    • pyproject.toml
    • setup.py

Core capabilities

  • Access nested JSON values with string paths like user/profile/name
  • Access array items with index keys like [0], [1]
  • Use special array symbols:
    • [^] first element (or prepend on set(..., force=True))
    • [$] last element (or append on set(..., force=True))
  • Optional default values for safe reads
  • Optional force=True writes to create compatible intermediate containers
  • File I/O helpers:
    • from_file(filename)
    • to_file(filename, indent)

Path syntax

  • Segments are separated by /
  • Object keys are plain strings, for example: settings/theme
  • Array indices are bracketed, for example: users/[0]/name
  • Valid index symbols: [0], [1], ..., [^], [$]

Examples

1) Basic get/set

from pyjsonobject import JsonObject

obj = JsonObject(json_str='{"user":{"name":"Ada","age":36}}')

obj.set("user/name", "Ada Lovelace")
name = obj.get("user/name")

2) Defaults and compatibility checks

from pyjsonobject import JsonObject

obj = JsonObject(json_str='{"user":{"name":"Ada"}}')

# Missing key in a compatible object path -> returns default
city = obj.get("user/city", default="unknown")

# Incompatible path still raises an error (object vs array mismatch)
# obj.get("user/[0]", default="fallback")

3) Array operations with special symbols

from pyjsonobject import JsonObject

arr = JsonObject(json_str='[1,2,3]')

arr.set("[^]", 0, force=True)  # prepend -> [0,1,2,3]
arr.set("[$]", 4, force=True)  # append  -> [0,1,2,3,4]

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

4) Force-create intermediate structures

from pyjsonobject import JsonObject

obj = JsonObject(json_str='{}')

# Without force this raises due to missing path/container
obj.set("a/[0]/name", "node-0", force=True)

5) Using JsonKeyPath directly

from pyjsonobject import JsonObject
from pyjsonobject.json_key_path import JsonKeyPath

obj = JsonObject(json_str='{"items":[{"id":7}]}')
path = JsonKeyPath("items/[0]/id")

value = obj.get(path.key_list())

6) Load and write files

from pyjsonobject import JsonObject

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

Install

From source (editable)

cd /home/dkybelksties/Repos/Github/PyJsonObject
python3 -m pip install -e .

Build a wheel

cd /home/dkybelksties/Repos/Github/PyJsonObject
python3 -m pip install build
python3 -m build

Run tests

cd /home/dkybelksties/Repos/Github/PyJsonObject
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.1.1.tar.gz (21.5 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.1.1-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kingkybel_pyjsonobject-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fce2eff22cce9365fa7a8591d59ce3608cdb8cc3886d5c6a5f0b6cbc18fe5afe
MD5 1644ce636bff7e68011e556476f8c1cd
BLAKE2b-256 f352fe3aada200f13c74facae92d33ddf5f282efe1522f36ae9995745dea41a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kingkybel_pyjsonobject-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8535baf151114e04781f68820bd431e753fb34eed0562995facd9f9ac57b67c8
MD5 bef8de5fccf6c35c8c758074c0d73136
BLAKE2b-256 3348b3ec823761ba06574ccda7837af0173f1bf161c6b33b623eb8eb821d5562

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