How to save data in JSON format
• 1 minJSON is an industry standard for sharing data across the web in a human-readable format.
In Python, a JSON is really close to a dictionary.
A JSON file would look like this
In Python
Saving a dictionary to a JSON file
There is a Python-native library called json that will help you deal with JSON files in Python.
It is native because it's already included in The Python Standard Library and you won't need to install it.
Here is an example :
# We import the json library
import json
# We create our example dictionary
my_dict = {
"firstname" : "Brice",
"lastname" : "Doe",
"age" : 20,
"has_siblings" : True
}
# We create a new file with open() with the
# writing mode specify with "w"
# And use the json dump method that will
# write the content of my_dict into the file
with open("my_json_file.json", "w") as file:
json.dump(my_dict, file)
Here you are! You now know how to write a JSON file in Python using the json library.