glom
glom is a declarative object transformer and formatter for Python, designed to simplify working with nested data structures. It provides path-based access, powerful data transformation using Pythonic specifications, meaningful error messages, and built-in debugging features. The library follows a CalVer versioning scheme (YY.MM.MICRO) and is actively maintained, with version 25.12.0 being a recent release that includes maintenance updates and test fixes for newer Python versions.
Warnings
- breaking The `*` and `**` wildcard behavior for path access became enabled by default in glom 23.1.0. This can break existing specs that used these characters as literal keys.
- deprecated The `Call` specifier is deprecated in favor of the more comprehensive `Invoke` specifier, introduced in glom 19.10.0. `Invoke` offers a richer API for invoking callables within your specs.
- gotcha Not all internal components of the `glom` package are part of its public API. Functionality not explicitly documented in the top-level `glom` package or its official documentation may change or disappear without notice.
- gotcha If `glom()` is called without `default` and `skip_exc` parameters, it will raise exceptions on path access failures (e.g., `PathAccessError` for missing keys/attributes).
- breaking Official support for Python 2 and Python 3.6 was dropped in glom 23.3.0, and pre-Python 3.7 syntax was removed in 24.11.0.
Install
-
pip install glom
Imports
- glom
from glom import glom
- T
from glom import T
- Coalesce
from glom import Coalesce
- Invoke
from glom import Invoke
- Path
from glom import Path
- S
from glom import S
Quickstart
from glom import glom, T
data = {
'user': {
'profile': {
'name': 'Alice',
'email': 'alice@example.com'
},
'preferences': {
'theme': 'dark'
}
},
'messages': [
{'id': 1, 'text': 'Hello'},
{'id': 2, 'text': 'World'}
]
}
# Extract a deeply nested value
user_name = glom(data, 'user.profile.name')
print(f"User Name: {user_name}")
# Transform data to a new structure
user_summary_spec = {
'name': 'user.profile.name',
'contact_email': 'user.profile.email',
'first_message': ('messages.0.text', str.upper)
}
user_summary = glom(data, user_summary_spec)
print(f"User Summary: {user_summary}")
# Using T for more complex access or method calls
message_ids = glom(data, ('messages', [T['id']]))
print(f"Message IDs: {message_ids}")