Claude Code for glom: Nested Data Access in Python — Claude Skills 360 Blog
Blog / AI / Claude Code for glom: Nested Data Access in Python
AI

Claude Code for glom: Nested Data Access in Python

Published: April 9, 2028
Read time: 5 min read
By: Claude Skills 360

glom accesses, transforms, and validates nested Python data structures. pip install glom. Basic: from glom import glom; glom(data, "a.b.c") — dot-path into nested dicts. glom(data, ("a", "b")) — tuple path. Path: from glom import Path; glom(data, Path("a", 0, "name")). Default: glom(data, "x.y", default=None). Coalesce: from glom import Coalesce; glom(data, Coalesce("a.b", "a.c", default="")). Assign: from glom import Assign; glom(data, Assign("a.b", 42)). T: from glom import T; glom(data, T.upper()) — call methods. Iter: from glom import Iter; glom(data, ("items", Iter("name").all())). Merge: from glom import Merge; glom([{"a":1}, {"b":2}], Merge()). Match/Check: from glom import Match, Check; glom(data, Match({"name": str, "age": Check(int, gt=0)})). Fill: from glom import Fill; glom(data, Fill({"id": "user.id", "label": T.upper()})). Auto: glom(data, Auto(target=MyDataclass)). Flatten: from glom import flatten; flatten([[1,2],[3]]). Register: register(MyType, glom_op). glom(spec=...). Auto-spec via glom.glom. Claude Code generates glom extraction pipelines, nested API response parsers, and data reshaping utilities.

CLAUDE.md for glom

## glom Stack
- Version: glom >= 23.5 | pip install glom
- Access: glom(data, "a.b.c") | glom(data, ("a", 0, "name"))
- Default: glom(data, "x.y", default=None)
- Coalesce: Coalesce("path1", "path2", default=val) — first non-missing wins
- Assign: Assign("a.b", value) — deep mutation
- T: glom(obj, T.method().attr) — method chaining on nested values
- Iter: glom(data, ("list_key", Iter("field").all())) — transform nested lists

glom Nested Data Pipeline

# app/glom_utils.py — glom path access, coalesce, assign, iter, match, fill, merge
from __future__ import annotations

from typing import Any

from glom import (
    Assign,
    Check,
    Coalesce,
    Fill,
    Iter,
    Match,
    Merge,
    Path,
    T,
    TType,
    flatten,
    glom,
)


# ─────────────────────────────────────────────────────────────────────────────
# 1. Core access helpers
# ─────────────────────────────────────────────────────────────────────────────

def get(obj: Any, path: str, default: Any = None) -> Any:
    """
    Access a nested value by dot-path.
    Returns default if any key is missing.

    Example:
        get({"a": {"b": 42}}, "a.b")   → 42
        get({"a": {}},        "a.b")   → None
    """
    return glom(obj, path, default=default)


def get_path(obj: Any, *keys: str | int, default: Any = None) -> Any:
    """
    Access a nested value by explicit key sequence.
    Supports mixed string/int keys for list indexing.

    Example:
        get_path(data, "users", 0, "email")
    """
    return glom(obj, Path(*keys), default=default)


def coalesce(obj: Any, *paths: str, default: Any = None) -> Any:
    """
    Return the first path that resolves to a non-missing value.

    Example:
        coalesce(r, "user.display_name", "user.username", "user.id")
    """
    return glom(obj, Coalesce(*paths, default=default))


def pluck(obj: Any, *paths: str, default: Any = None) -> dict[str, Any]:
    """
    Extract multiple paths from obj into a flat dict.

    Example:
        pluck(user, "id", "profile.email", "settings.theme")
        → {"id": 1, "profile.email": "[email protected]", "settings.theme": "dark"}
    """
    return {path: get(obj, path, default=default) for path in paths}


# ─────────────────────────────────────────────────────────────────────────────
# 2. Mutation helpers
# ─────────────────────────────────────────────────────────────────────────────

def assign(obj: Any, path: str, value: Any) -> Any:
    """
    Set a nested value in obj (in-place). Returns obj.

    Example:
        assign(config, "database.pool_size", 10)
    """
    glom(obj, Assign(path, value))
    return obj


def assign_many(obj: Any, updates: dict[str, Any]) -> Any:
    """Apply multiple path → value assignments in sequence."""
    for path, value in updates.items():
        assign(obj, path, value)
    return obj


# ─────────────────────────────────────────────────────────────────────────────
# 3. List / Iter transformations
# ─────────────────────────────────────────────────────────────────────────────

def pluck_list(items: list[Any], path: str) -> list[Any]:
    """
    Extract a field from every item in a list.

    Example:
        pluck_list(users, "email") → ["[email protected]", "[email protected]", ...]
    """
    return [get(item, path) for item in items]


def collect(obj: Any, list_path: str, field_path: str) -> list[Any]:
    """
    Traverse list_path then collect field_path from each element.

    Example:
        collect(data, "orders", "total") → [9.99, 19.99, ...]
    """
    return glom(obj, (list_path, Iter(field_path).all()))


def transform_list(obj: Any, list_path: str, spec: Any) -> list[Any]:
    """Apply any glom spec to each element of a nested list."""
    return glom(obj, (list_path, Iter(spec).all()))


def reshape(items: list[dict], mapping: dict[str, str]) -> list[dict]:
    """
    Reshape a list of dicts by remapping keys.
    mapping: {new_key: source_path, ...}

    Example:
        reshape(rows, {"name": "first_name", "email": "contact.email"})
    """
    result = []
    for item in items:
        result.append({new_key: get(item, src) for new_key, src in mapping.items()})
    return result


def flatten_nested(obj: Any, path: str) -> list[Any]:
    """Flatten a nested list at path into a single list."""
    nested = get(obj, path, default=[])
    return flatten(nested)


def group_by(items: list[dict], key_path: str) -> dict[Any, list[dict]]:
    """Group a list of dicts by the value at key_path."""
    groups: dict[Any, list[dict]] = {}
    for item in items:
        k = get(item, key_path)
        groups.setdefault(k, []).append(item)
    return groups


# ─────────────────────────────────────────────────────────────────────────────
# 4. Fill — template-based extraction
# ─────────────────────────────────────────────────────────────────────────────

def extract(obj: Any, template: dict[str, Any] | list[Any]) -> Any:
    """
    Build a new structure from obj using a template.
    Template values can be path strings or glom specs.

    Example:
        extract(user, {
            "id":    "id",
            "email": "contact.email",
            "name":  T["first"] + " " + T["last"],
        })
    """
    def _resolve(spec):
        if isinstance(spec, str):
            return get(obj, spec)
        return glom(obj, spec)

    if isinstance(template, dict):
        return {k: _resolve(v) for k, v in template.items()}
    return [_resolve(v) for v in template]


# ─────────────────────────────────────────────────────────────────────────────
# 5. Merge helpers
# ─────────────────────────────────────────────────────────────────────────────

def merge_dicts(*dicts: dict) -> dict:
    """
    Deep-merge multiple dicts using glom Merge.
    Later keys win over earlier ones.
    """
    return glom(list(dicts), Merge())


def merge_defaults(base: dict, defaults: dict) -> dict:
    """Apply defaults to base — base values win (like dict.setdefault for nested keys)."""
    return merge_dicts(defaults, base)


# ─────────────────────────────────────────────────────────────────────────────
# 6. Match / validation
# ─────────────────────────────────────────────────────────────────────────────

def validate(obj: Any, schema: Any) -> tuple[bool, str]:
    """
    Validate obj against a glom Match schema.
    Returns (True, "") on success or (False, error_message).

    Example:
        validate(user, {"name": str, "age": Check(int, gt=0)})
    """
    try:
        glom(obj, Match(schema))
        return True, ""
    except Exception as exc:
        return False, str(exc)


def must_match(obj: Any, schema: Any) -> Any:
    """Validate and return obj; raise MatchError on failure."""
    return glom(obj, Match(schema))


# ─────────────────────────────────────────────────────────────────────────────
# 7. API response helpers
# ─────────────────────────────────────────────────────────────────────────────

def unwrap_response(
    response: dict,
    data_path: str = "data",
    meta_paths: list[str] | None = None,
) -> dict:
    """
    Extract data + optional metadata from a nested API response.

    Example response: {"data": {"users": [...]}, "meta": {"total": 50}}
    unwrap_response(resp, "data.users", meta_paths=["meta.total"])
    → {"data": [...], "total": 50}
    """
    result = {"data": get(response, data_path)}
    for mp in (meta_paths or []):
        key = mp.rsplit(".", 1)[-1]
        result[key] = get(response, mp)
    return result


def normalize_pagination(response: dict) -> dict:
    """
    Extract common pagination fields from various API response shapes.
    Handles: {total, page, per_page}, {count, next, previous}, {meta.total}.
    """
    return {
        "total":    coalesce(response, "total", "count", "meta.total", default=None),
        "page":     coalesce(response, "page",  "meta.page", default=1),
        "per_page": coalesce(response, "per_page", "limit", "meta.per_page", default=20),
        "next":     get(response, "next"),
        "previous": get(response, "previous"),
    }


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    print("=== Basic access ===")
    data = {"user": {"profile": {"name": "Alice", "age": 30}, "roles": ["admin", "user"]}}
    print("name:", get(data, "user.profile.name"))
    print("age:",  get(data, "user.profile.age"))
    print("missing:", get(data, "user.address.city", default="N/A"))

    print("\n=== Path (mixed keys) ===")
    data2 = {"orders": [{"id": 1, "total": 9.99}, {"id": 2, "total": 19.99}]}
    print("first total:", get_path(data2, "orders", 0, "total"))

    print("\n=== Coalesce ===")
    user = {"username": "bob", "display_name": None}
    print("label:", coalesce(user, "display_name", "username", default="Unknown"))

    print("\n=== Collect from list ===")
    catalog = {"products": [
        {"sku": "A1", "price": 4.99},
        {"sku": "B2", "price": 9.99},
        {"sku": "C3", "price": 14.99},
    ]}
    prices = collect(catalog, "products", "price")
    print("prices:", prices)

    print("\n=== Reshape ===")
    rows = [
        {"first_name": "Alice", "last_name": "Smith",  "contact": {"email": "[email protected]"}},
        {"first_name": "Bob",   "last_name": "Jones",  "contact": {"email": "[email protected]"}},
    ]
    reshaped = reshape(rows, {"name": "first_name", "email": "contact.email"})
    for r in reshaped:
        print(f"  {r}")

    print("\n=== Pluck (multi-path) ===")
    profile = {"id": 42, "profile": {"email": "[email protected]"}, "settings": {"theme": "dark"}}
    extracted = pluck(profile, "id", "profile.email", "settings.theme")
    print(extracted)

    print("\n=== Assign (deep set) ===")
    config = {"database": {"host": "localhost", "port": 5432}}
    assign(config, "database.port", 5433)
    print("Updated port:", config["database"]["port"])

    print("\n=== Merge ===")
    defaults = {"debug": False, "workers": 4, "log_level": "INFO"}
    overrides = {"workers": 8, "log_level": "DEBUG"}
    merged = merge_defaults(overrides, defaults)
    print("Merged:", merged)

    print("\n=== Validate ===")
    schema = {"name": str, "age": Check(int, gt=0)}
    ok, err = validate({"name": "Alice", "age": 30}, schema)
    print("Valid:", ok)
    ok2, err2 = validate({"name": "Bob",   "age": -1}, schema)
    print("Invalid:", ok2, err2[:60])

    print("\n=== API response unwrap ===")
    api_resp = {"data": {"users": [{"id": 1}, {"id": 2}]}, "meta": {"total": 2}}
    result = unwrap_response(api_resp, "data.users", meta_paths=["meta.total"])
    print(result)

For the jmespath alternative — jmespath implements the JMESPath query language for JSON, which is powerful for filtering and projecting JSON arrays; glom is Python-native (works on any object, not just JSON-serializable dicts), supports assignment, method chaining via T, Match validation, and Coalesce fallback in a single library. For the jsonpath-ng alternative — jsonpath-ng implements RFC JSONPath and is good for standards-compliant JSON querying; glom is Pythonic and integrates tightly with dataclasses, Pydantic models, and arbitrary Python objects in ways that JSONPath doesn’t, making it better for internal data pipelines. The Claude Skills 360 bundle includes glom skill sets covering glom() dot-path access, get()/get_path() helpers, coalesce() fallback chains, pluck() multi-path extraction, assign()/assign_many() deep mutation, collect()/transform_list() nested list ops, reshape() key remapping, flatten_nested(), group_by(), extract() template-based extraction, merge_dicts()/merge_defaults(), validate()/must_match() schema checking, unwrap_response() and normalize_pagination() API helpers. Start with the free tier to try nested data transformation code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free