Skip to content

Instantly share code, notes, and snippets.

@mikegrima
Last active October 14, 2016 22:14
Show Gist options
  • Save mikegrima/5ffb717d57a04be9bef0d3bd297b877f to your computer and use it in GitHub Desktop.
Save mikegrima/5ffb717d57a04be9bef0d3bd297b877f to your computer and use it in GitHub Desktop.
Quick and dirty recursive dict lowercaser
import json
"""
This is NOT extensive in any way -- just something quick and dirty. It doesn't handle all datatypes -- use at your own risk.
"""
def lowercase(original):
if isinstance(original, str):
return original.lower()
elif isinstance(original, list):
new_list = []
for item in original:
new_list.append(lowercase(item))
return new_list
elif not original:
return None
# Catch-all? Hopefully? Maybe?
elif not isinstance(original, dict):
return original
staging_dict = {}
for item, data in original.iteritems():
lower_cased = lowercase(data)
staging_dict[item.lower()] = lower_cased
return staging_dict
asdf = {
"SomeValue": "IsThis",
"SeCondV": "SoemthingElse",
"Third": {
"Nested": {
"AnotherNested": "One",
"Two": "AnotherNested"
},
"Three": "Four"
},
"Fourth": {
"Some Value": [
"One",
"twO",
{
"Three": "Four",
"Five": [
"6",
"7",
"8",
9,
10,
True,
-10202.33982
]
}
]
},
"Fifth": None
}
'''
Outputs:
$ python lowercaser.py
{
"third": {
"three": "four",
"nested": {
"anothernested": "one",
"two": "anothernested"
}
},
"somevalue": "isthis",
"secondv": "soemthingelse",
"fifth": null,
"fourth": {
"some value": [
"one",
"two",
{
"five": [
"6",
"7",
"8",
9,
10,
true,
-10202.33982
],
"three": "four"
}
]
}
}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment