The full form of JSON is Javascript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within < >. It is similar to the dictionary in Python.
Serializing JSON 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() or dumps() function to convert the Python objects into their respective JSON object, so it makes it easy to write data to files. See the following table given below.
PYTHON OBJECT | JSON OBJECT |
---|---|
Dict | object |
list, tuple | array |
str | string |
int, long, float | numbers |
True | true |
False | false |
None | null |
The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object. It takes two parameters:
After converting the dictionary to a JSON object, simply write it to a file using the “write” function.