How to save data in JSON format
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
JSON 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.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚