Skip to content

Instantly share code, notes, and snippets.

@IanLeatherbury
Last active February 22, 2018 23:51
Show Gist options
  • Save IanLeatherbury/a11d35e3dc43bbf1f9c5fce65bbf95ed to your computer and use it in GitHub Desktop.
Save IanLeatherbury/a11d35e3dc43bbf1f9c5fce65bbf95ed to your computer and use it in GitHub Desktop.
json_string = '{"first_name": "Ian", "second_name":"Leatherbury"}'
class User(object):
def __init__(self, first_name, second_name):
self.first = first_name
self.second = second_name
# Deserialize
j = json.loads(json_string)
# Instantiate with **kwargs
u = User(**j)
print("Second Name: " + u.first)
# prints: Second Name: Ian
# Serialize
ob = json.dumps(u, default=lambda o: o.__dict__)
print("Ob: " + ob)
# Ob: {"first": "Ian", "second": "Leatherbury"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment