Last active
March 19, 2017 10:54
-
-
Save yagizdemirsoy/066ceacbcf1f05e8be135ab20ac7ebdd to your computer and use it in GitHub Desktop.
Python 3 - Traverse json recursively and capitalize first letters of each key.
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
import simplejson as json | |
import json | |
def main(): | |
with open('input.json', encoding='utf-8') as json_data: | |
data = json.load(json_data) | |
d = capitalize(data) | |
print(json.dumps(d, indent=4, sort_keys=True)) | |
def capitalize(x): | |
if isinstance(x, list): | |
return [capitalize(v) for v in x] | |
elif isinstance(x, dict): | |
return {k[0].upper() + k[1:]: capitalize(v) for k, v in x.items()} | |
else: | |
return x | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment