Skip to main content

Attribute-style access wrapper for JSON-like data with dot-path set/delete and safe defaults

Project description

json-object-mapper

json-object-mapper is a lightweight Python library that lets you interact with JSON data using attribute-style access instead of dictionary keys.

The PyPI distribution is published as json-object-mapper.

It turns JSON objects into JSONObjectMapper instances that feel natural to use in Python code, while preserving full JSON compatibility.

from json_object_mapper import JSONObjectMapper

data = {
    "user": {"name": "Kobby", "age": 29, "skills": ["python", "aws", "forex"]}
}
obj = JSONObjectMapper(data)
print(obj.user.name)        # "Kobby"
print(obj.user.skills[0])   # "python"
print(obj.to_json(indent=2))

✨ Features

  • Attribute-style access (obj.key) for dict keys
  • Recursive wrapping for nested dicts and lists
  • Read-only mode (immutability enforced)
  • Dot/bracket path lookups (obj.get_path("a.b[0].c"))
  • New: set_path() / del_path() for dot paths
  • New: default_factory + autocreate_missing for safe defaults
  • Utility methods: to_dict, to_json, from_json, merge

Install

pip install json-object-mapper

Usage

from json_object_mapper import JSONObjectMapper, JSONAccessError

# 1) Wrap & read (basic)
obj = JSONObjectMapper({"user": {"name": "Kobby", "age": 29}})
assert obj.user.name == "Kobby"
assert obj.user.age == 29

# 2) Write with dot notation
obj.user.country = "GH"
assert obj.user.country == "GH"

# 3) Lists of dicts (read & write via attribute access)
obj.services = [{}]  # start with a list containing one dict
obj.services[0].name = "auth"  # item is wrapped → dot works
obj.services[0].enabled = True
assert obj.services[0].name == "auth"
assert obj.services[0].enabled is True

# 4) Non-identifier keys (use mapping-style access)
# Keys like "first-name" can't be attributes; use get()/[] instead
obj.meta = {"first-name": "Kobby"}
assert obj.meta.get("first-name") == "Kobby"
assert obj.meta["first-name"] == "Kobby"
# getattr(obj.meta, "first-name") would raise JSONAccessError

# 5) Safe defaults + auto-create (no extra helpers required)
# Missing attributes produce defaults; with autocreate they persist.
cfg = JSONObjectMapper({}, default_factory=dict, autocreate_missing=True)
cfg.profile.settings.theme = "dark"  # on-demand creation of nested dicts
assert cfg.profile.settings.theme == "dark"

# 6) Merge convenience (shallow merge into root dict)
cfg.merge({"features": {"beta": True}})
assert cfg.features.beta is True

# 7) Read-only wrappers (safe reads; writes raise)
ro = JSONObjectMapper({"debug": True}, readonly=True)
assert ro.debug is True
try:
    ro.debug = False
    raise AssertionError("should not be able to write in readonly mode")
except AttributeError:
    pass

Tests

python -m pip install -e .
python -m unittest discover -s tests -v

Publishing

Publishing is automated with GitHub Actions using PyPI trusted publishing. Create a GitHub release after configuring the pypi environment in the repository settings.

MIT License.

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

json_object_mapper-2.0.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

json_object_mapper-2.0.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file json_object_mapper-2.0.0.tar.gz.

File metadata

  • Download URL: json_object_mapper-2.0.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for json_object_mapper-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b9a0d79df0eaa773e2746efd4bb9334cb8afeb2d71066faa856cc83fc2384211
MD5 5f419a9ca59538ef986789f1614ae485
BLAKE2b-256 5903d5014bcd9655ffe969656b242ffa8c444eba6652b8c7464de6208aafd4b4

See more details on using hashes here.

File details

Details for the file json_object_mapper-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_object_mapper-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e9a37aabb6bc55ad4a5879b43e43bea6bf4155332e76437987034aea3702ae2
MD5 2cf1e320532d5970c27aea576affc63a
BLAKE2b-256 d8d75946287ebf12b6bacc7b07df7812eca2a6c6fa020ec6beb3a5019478e232

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