from typing import Union


def _get_item(ptr, itempath: str) -> Union[str, dict, list, None]:
    """Utility function. The ptr param should point at .services then follow the itempath (separated via '.') to
    the expected object. Returns None if invalid (tries to avoid Error)."""
    root = ptr
    for element in itempath.split('.'):
        if isinstance(root, dict):
            if element in root:
                root = root[element]
            else:
                return None
        elif isinstance(root, list):
            index = int(element)
            if not (0 <= index < len(root)):
                return None
            root = root[index]
        else:
            return None
    return root