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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly