Convert dict keys to snake case
Level: Advanced (score: 4)
In this Bite we will take a (nested) dict
and transform its (string) keys to snake case.
So for example this dictionary:
{
"camelCase": "Value1",
"camelcase": "Value1",
"PascalCase": "Value2",
"kebab-case": "Value3",
"ACRONYM": "Value4",
"number22": "Value5"
}
Would result in the following dictionary where the keys have been converted into snake_case:
{
"camel_case": "Value1",
"camelcase": "Value1",
"pascal_case": "Value2",
"kebab_case": "Value3",
"a_c_r_o_n_y_m": "Value4",
"number_22": "Value5"
}
As you can see from this example hyphens, numbers and acronyms trigger snake casing as well.
Additionally, it should work with nested data structures as well. For example a dict
of dict
s and a dict
where the values contain a (nested) list
.