How to save data in JSON format

1 min readJsonData ManipulationData
7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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

{
    "firstname" : "Brice",
    "lastname" : "Doe",
    "age" : 20,
    "has_siblings" : true
}
A JSON file would look like this

In Python

my_dict = {
    "firstname" : "Brice",
    "lastname" : "Doe",
    "age" : 20,
    "has_siblings" : True
}
In Python, a dict would look like this

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.

7-Day Challenge

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.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

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. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.