Skip to main content

Dot notation access for dict/JSON with safety fallback.

Project description

Problem

When working with deeply nested dictionaries or JSON data in Python, accessing fields using standard dict syntax (e.g., data["user"]["profile"]["name"]) can be verbose and error-prone. If any key in the chain is missing, a KeyError is raised, which can break your code or require lots of try/except or .get() calls. This makes code harder to read and maintain, especially when dealing with data from APIs or user input where fields may be missing or optional.

safe_parse solves this by allowing you to safely access any depth of nested data using dot notation. Missing fields automatically return None (even if you access attributes of None, it will still return None instead of raising an error), so your code stays clean, readable, and robust—no more KeyErrors or repetitive checks.

safe_parse

A modern Python package for safe, intuitive dot notation access to dictionaries and JSON data. Gracefully returns None for missing fields, supports deep/nested access, and enables robust, error-free data handling—including safe chaining of attribute access at any depth.

Installation

pip install safe-parse

Usage

Basic Example

from safe_parse import SafeParse

payload = {"name": "Alice"}
obj = SafeParse(payload)
print(obj.name)  # Alice
print(obj.age)   # None
print(obj.age == None) # True

Nested Access

payload = {
	"user": {
		"profile": {
			"name": "Bob",
			"age": 30
		},
		"settings": {
			"theme": "dark"
		}
	}
}
obj = SafeParse(payload)
print(obj.user.profile.name)      # Bob
print(obj.user.profile.age)       # 30
print(obj.user.settings.theme)    # dark
print(obj.user.profile.gender)    # None

Using get() with Default

payload = {"x": 10}
obj = SafeParse(payload)
print(obj.get("x"))        # 10
print(obj.get("y", 42))    # 42

Convert Back to dict

payload = {"foo": "bar"}
obj = SafeParse(payload)
print(obj.to_dict())        # {'foo': 'bar'}

Iterating Keys, Values, Items

payload = {"a": 1, "b": 2}
obj = SafeParse(payload)
print(list(obj.keys()))     # ['a', 'b']
print(list(obj.values()))   # [1, 2]
print(list(obj.items()))    # [('a', 1), ('b', 2)]

Boolean and Equality Checks

obj = SafeParse({})
print(obj.missing_field)        # None
print(bool(obj.missing_field))  # False
print(obj.missing_field == None) # True

Safe Chaining

obj = SafeParse({})
print(obj.not_found.anything.deeply.nested)  # None

License

MIT

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

safe_parse-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

safe_parse-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file safe_parse-1.0.0.tar.gz.

File metadata

  • Download URL: safe_parse-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for safe_parse-1.0.0.tar.gz
Algorithm Hash digest
SHA256 612e6a07ca0953871fadedf9594d0f1355a57de8ea61cc65cae8cb9faa0e8b51
MD5 285c7a8e822677645814c8d4be3a117e
BLAKE2b-256 8f0a2c1522f940e583d08695e96eec104864e1ebdb33e019af7f86d9a4832f90

See more details on using hashes here.

File details

Details for the file safe_parse-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: safe_parse-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for safe_parse-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d0846eafe8b3649d26da0d348832750535fbc09a0c8ac6a5886bac0018a4824
MD5 0a9471d6f33df4bb3fb4bbb50eff4efc
BLAKE2b-256 0669be9fab2cbe0aee87f505f6d85ca192bef0eeb589d587cac2900b68b318a2

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