Skip to content

Instantly share code, notes, and snippets.

@yagizdemirsoy
Last active January 27, 2025 05:34
Show Gist options
  • Select an option

  • Save yagizdemirsoy/066ceacbcf1f05e8be135ab20ac7ebdd to your computer and use it in GitHub Desktop.

Select an option

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