← Back to course
Code Crew G6+ / Week 09 / Tuesday
2/6
Week 09 Β· Structured data

Tuesday

Writing JSON with json.dump
// Saving lists and dictionaries with JSON
⏱ about 30 min

Tuesday: Writing JSON with json.dump

Now that Rocket has seen JSON, he wants the crew list saved in a file, with every key and value kept. He remembers how much work the weather log took, splitting every line by commas.

"Do I have to write every bracket and quote mark myself?" he asks, worried.

Raven shakes her head and smiles. "What do you notice about the shell yesterday?" she asks. "json.dumps did all the brackets for us. Maybe there is a version that writes to a file."

Nova shows a file icon filling neatly with indented lines. "I found a pattern," she says. "dumps makes a string. dump, without the s, writes straight into a file. Add indent to make it easy to read."

Rocket types json.dump and saves the whole crew in one line.

json.dump

json.dump(data, f) writes your data into an open file as JSON. Open the file with "w", because the new JSON should replace the old file, not be added to the end.

indent=2 puts each item on its own line, indented two spaces per level. Without it, everything goes on one long line. Both work, but people prefer reading the indented version.

# save_crew.py
import json

crew = [
    {"name": "Rocket", "role": "engineer", "missions": 3},
    {"name": "Raven", "role": "explorer", "missions": 4},
]
with open("crew.json", "w") as f:
    json.dump(crew, f, indent=2)
print("Saved", len(crew), "crew records")

The shell shows Saved 2 crew records. This is what crew.json holds:

[
  {
    "name": "Rocket",
    "role": "engineer",
    "missions": 3
  },
  {
    "name": "Raven",
    "role": "explorer",
    "missions": 4
  }
]
How many records (dictionaries) does crew.json hold?
What mode is crew.json opened with in save_crew.py?
WHY "W" AND NOT "A"?
  • Read the question.
  • Tap your answer.
You run save_crew.py twice with mode "a". What would crew.json hold?
What does indent=2 change?
At your computer
1. Type save_crew.py, save it in your mission folder and run it.
2. In IDLE choose File, then Open, and open crew.json. You may need to choose to show all files.
3. Compare the file with the dictionaries in your program.
4. Change indent=2 to indent=4, run it again, and look at the file.
5. Add a third crew member to the list in your program and run it.

Practice: save a settings file

Many apps save their settings as a JSON object. Here a dictionary of settings is written to settings.json.

# save_settings.py
import json

settings = {"sound": True, "volume": 7, "theme": "night"}
with open("settings.json", "w") as f:
    json.dump(settings, f, indent=2)
print("Saved", len(settings), "settings")
{
  "sound": true,
  "volume": 7,
  "theme": "night"
}
READ THE SETTINGS FILE
  • Read the question.
  • Tap your answer.
Why does the file say true and not True?
What does the shell show?
Why is each setting on its own line in the file?
How many spaces start the line "volume": 7 in the file?
You add "language": "en" to settings. What does the shell show? Saved ____ settings
What JSON word would a value of None become?
StatementTrue or false?
json.dump writes into an open file.?
json.dumps writes into an open file.?
A file for json.dump should usually be opened with "w".?
indent changes the values saved in the file.?
WHY THIS EXERCISEdump writes to a file and dumps makes a string, while indent only changes how the file looks.

One line saved a whole list of dictionaries. Tomorrow you will load it back.

← Monday