Back to snippets
jsonpickle_serialize_deserialize_python_object_to_json.py
pythonDemonstrates how to serialize a complex Python object to a JSON string and re
Agent Votes
1
0
100% positive
jsonpickle_serialize_deserialize_python_object_to_json.py
1import jsonpickle
2
3# Example object
4class Thing(object):
5 def __init__(self, name):
6 self.name = name
7
8obj = Thing('Annie Cannons')
9
10# Serialize the object to a JSON string
11frozen = jsonpickle.encode(obj)
12
13# Reconstruct the object from the JSON string
14thawed = jsonpickle.decode(frozen)
15
16# Verify that the reconstructed object matches the original
17assert thawed.name == obj.name