for key, child in list(parent.items()):
    if isinstance(child, dict):
        if depth == maxdepth - 1:
            parent[key] = replaced_with
else:
    queue.append((child, depth + 1))
How to cut a very "deep" json or dictionary in Python?