Last active
December 9, 2019 07:45
-
-
Save msguner/e44bb1e13efad092e301888e49f1dd91 to your computer and use it in GitHub Desktop.
Python transform json keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transformJsonKeys(obj, func=(lambda x: x)): | |
if isinstance(obj, list): | |
return [transformJsonKeys(element, func) for element in obj] | |
elif isinstance(obj, dict): | |
return {func(key): transformJsonKeys(value, func) for key, value in obj.items()} | |
else: | |
return obj | |
dic = {'k1': 1, 'k2': 2, 'k3': [{'k31': 31, 'k2': 32, 'k33': 33}]} | |
f = lambda x: "*" + x.capitalize() | |
print(transformJsonKeys(dic,f)) | |
#{'*K1': 1, '*K2': 2, '*K3': [{'*K31': 31, '*K32': 32, '*K33': 33}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment