Skip to content

Instantly share code, notes, and snippets.

@yagizdemirsoy
Last active March 19, 2017 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yagizdemirsoy/066ceacbcf1f05e8be135ab20ac7ebdd to your computer and use it in GitHub Desktop.
Save yagizdemirsoy/066ceacbcf1f05e8be135ab20ac7ebdd to your computer and use it in GitHub Desktop.
Python 3 - Traverse json recursively and capitalize first letters of each key.
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