Serializing JSON
The process of encoding JSON is usually called serialization. This term refers to
the transformation of data into a series of bytes (hence serial) to be stored
or transmitted across a network. To handle the data flow in a file, the JSON
library in Python uses dump() function
to convert the Python objects into their respective JSON object, so it makes
easy to write data to files.
See the following table given
below.
PYTHON OBJECT | JSON OBJECT |
dict | object |
list, | array |
str | string |
int, | numbers |
True | true |
False | false |
None | null |
Serialization Example :
Consider the given example of a Python object.
var = {
"Subjects": {
"Maths":85,
"Physics":90
}
}
Sample.json
and open it with write mode.
with open("Sample.json", "w") as p:
json.dumps(var, p)
Here, the dumps()
takes two arguments first, the data object to be serialized and second the object to which it will be written(Byte format).
Comments