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.1.0.tar.gz (4.5 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.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for safe_parse-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7e6e7fa292bb2a48b1f0c16005e0e1979e8f860f59810de2a9a71d1a5358cac5
MD5 cd8c59026c58641da585f2484097e42d
BLAKE2b-256 42d102e29beab152ac6444801a9234aafd2e75aa5ebfe9c1ceac73d606320364

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for safe_parse-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35edcd0bdd9caef8ffca93336af77bddb17c0deeb30eda68da9159c09e95d39b
MD5 d12540cef55d02817b7fb4f672576857
BLAKE2b-256 b5fcf007286a3324f5f8c38266e4a2c3f51015f04020ec4f66d4efad920c85db

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