Last active
February 22, 2018 23:51
-
-
Save IanLeatherbury/a11d35e3dc43bbf1f9c5fce65bbf95ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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