misc_json_etc Flashcards
1
Q
you have a python object - dict for example, how do you load it into JSON, i.e. serialization?
A
json.dump(data, write_file)
string = json.dumps(data)
2
Q
you have json data (represented as string) and you want to load it into a python object, i.e. deserialization.
A
load from string:
object = json.loads(json_object)
load from file
object = json.load(read_file)
3
Q
what if you want to serialize custom object from python?
A
string = json.dumps(data, default=target_function)
target_function returns objects which python can serialize, like a list, dic, etc.
4
Q
what if you want to de serialize your custom data?
A
json.loads(data, object_hook=decode_complex)
decode_complex is a function which returns a complex object