dictutils.ops.rename_keysΒΆ

dictutils.ops.rename_keys(d, mapping)[source]ΒΆ

Rename keys using path mapping.

Parameters:
  • d (Any) – Object to modify

  • mapping (Mapping[str, str]) – Dictionary mapping old paths to new paths

Return type:

Any

Returns:

The modified object

Example

>>> import json
>>> data = {"user": {"firstName": "Alice", "lastName": "Smith"}}
>>> rename_keys(data, {
...     "user.firstName": "user.name.first",
...     "user.lastName": "user.name.last"
... })
>>> print(json.dumps(data, indent=4))
{
    "user": {
        "name": {
            "first": "Alice",
            "last": "Smith"
        }
    }
}