JSON serialize a class and change property casing with Python

Viewed 2203

I'd like to create a JSON representation of a class and change the property names automatically from snake_case to lowerCamelCase, as I'd like to comply with PEP8 in Python and also the JavaScript naming conventions (and maybe even more importantly, the backend I'm communicating to uses lowerCamelCase).

I prefer to use the standard json module, but I have nothing against using another, open source library (e.g. jsonpickle might solve my issue?).

>>> class HardwareProfile:
...     def __init__(self, vm_size):
...             self.vm_size = vm_size
>>> hp = HardwareProfile('Large')
>>> hp.vm_size
'Large'
### ### What I want ### ###
>>> magicjson.dumps(hp)
'{"vmSize": "Large"}'
### ### What I have so far... ### ###
>>> json.dumps(hp, default=lambda o: o.__dict__)
'{"vm_size": "Large"}'
1 Answers
Related