</bugz loading .
JSON refers to JavaScript Object Notation. It is a light-weight data format that is used to store and transmit structured data. It is very similar to dictionaries in Python.
It is widely used to transfer data between web applications and servers. Because of its effectiveness in reducing data size, JSON is often considered suitable to store data. JSON has become a popular choice for structuring data in configuration files across various applications and platforms.
Since JSON and dictionaries in Python have a similar format, it's convenient for Python developers to work with JSON. To use JSON in Python, we should import the built-in library json.
To work with JSON data in Python, we will need to import the json module. It is a built-in module, so we don't need to install any external libraries.
We can import the json module by using the import keyword as shown below.
import json
To convert a Python dictionary to JSON, we can use the dumps() method of the json module.
json.dumps(python_dict)
import json
dict = {
"name": "Bugzee",
"city": "New York"
}
json_string = json.dumps(dict) # converting the dict to json string
print(json_string)
In the above code, we are creating a simple python dictionary dict.
Then we are converting the dictionary to a json string using the json.dumps() method.
{"name": "Bugzee", "city": "New York"}
To convert a Python dictionary to JSON file i.e to convert a python dictionary to a json object and write it to a json file, we can use the dump() method of the json module.
It is very similar to the json.dumps() method. The only difference between json.dump() and the json.dumps() method is that json.dump() method requires an extra parameter to specify the file in which the json string must be written.
json.dump(python_dict,file)
import json
dict = {
"name": "Bugzee",
"city": "New York"
}
# converting the dict to json string and writing it to the 'data.json' file
with open("data.json", "w") as file:
json.dump(dict, file)
In the above code, we are creating a simple python dictionary dict.
Then we are converting the dictionary to a json string and writing it to the data.json file using the json.dump() method.
{"name": "Bugzee", "city": "New York"}
# will be stored to the data.json file
json.dumps() and json.dump() methods have several useful parameters. They are used to customize the output json string.
Some of the most common parameters are :
It is used to show the hierarchy in json using spaces/tabs for better readability.
import json
dict = {
"name": "Bugzee",
"city": "New York"
}
with open("data.json", "w") as file:
json.dump(dict, file, indent=4) # adding indentation of spaces
{
"name": "Bugzee",
"city": "New York"
}
# will be saved to data.json file
In the above code, the dicitonary dict will be converted in to a json string with indentation of 4 spaces and will be saved in the data.json file.
It is used to sort the keys of the json data alphabetically.
import json
dict = {
"name": "Bugzee",
"city": "New York"
}
json_string = json.dumps(dict, sort_keys=True) # sorting the keys alphabetically
print(json_string)
{"city": "New York", "name": "Bugzee"}
In conclusion, we need to import the json module in order to work on JSON in python. A Python dictionary can be converted to a JSON string using the json.dumps() function, and the json.dump() function can be used to convert a python dictionary to a JSON string and save it to a JSON file. These methods offer parameters for the customization of the output JSON string such as indent, sort_keys, etc.