Skip to content

Instantly share code, notes, and snippets.

@codeadict
Created August 31, 2017 15:49
Show Gist options
  • Select an option

  • Save codeadict/13e8cfbb2e720145e31a96fc2f63aae1 to your computer and use it in GitHub Desktop.

Select an option

Save codeadict/13e8cfbb2e720145e31a96fc2f63aae1 to your computer and use it in GitHub Desktop.
Lower case Json decoder in Python3
class LowerCaseJSONDecoder(JSONDecoder):
def _lower_keys(self, obj):
new = {}
for k, v in obj.items():
if isinstance(v, dict):
v = self._lower_keys(v)
new[k.lower()] = v
return new
def decode(self, *args, **kwargs):
obj = super().decode(*args, **kwargs)
return self._lower_keys(obj)
@codeadict
Copy link
Author

Usage:

>>> import json
>>> txt = '{"SerialNumber": 45, "Name": "Super"}'
>>> payload = json.loads(txt, cls=LowerCaseJSONDecoder)
>>> payload
{'serialnumber': 45, 'name': 'Super'}
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment