Working with JSON files in Python - Mini Project: Build & Apply
Start learning this pattern below
Jump into concepts and practice - no test required
books that holds a list of dictionaries. Each dictionary should represent a book with these exact keys and values: {"title": "The Hobbit", "author": "J.R.R. Tolkien", "year": 1937} and {"title": "1984", "author": "George Orwell", "year": 1949}.Use a list with two dictionaries inside. Each dictionary has keys: title, author, and year.
json module. Then open a file called library.json in write mode and save the books list to this file using json.dump().Use import json. Then open the file with open("library.json", "w") and use json.dump(books, file) to save.
library.json in read mode and load the JSON data into a variable called loaded_books using json.load(). Then add a new book dictionary {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year": 1960} to loaded_books.Use with open("library.json", "r") as file and loaded_books = json.load(file). Then use loaded_books.append(...) to add the new book.
library.json in write mode again and save the updated loaded_books list using json.dump(). Then print loaded_books to show the updated list.Open the file with open("library.json", "w") and save loaded_books with json.dump(). Then print loaded_books.
Practice
What is the main purpose of using JSON files in Python programs?
Solution
Step 1: Understand JSON file usage
JSON files store data in a text format that is easy to read and share.Step 2: Identify the correct purpose
Saving and loading data simply matches the main use of JSON files in Python.Final Answer:
To save and load data in a simple text format -> Option CQuick Check:
JSON files = data storage [OK]
- Thinking JSON speeds up code execution
- Confusing JSON with GUI creation
- Assuming JSON compiles code
Which of the following is the correct way to open a JSON file named data.json for reading in Python?
Solution
Step 1: Understand file modes
'r' mode opens a file for reading, 'w' for writing, 'a' for appending, 'x' for creating new file.Step 2: Choose mode for reading JSON
To read JSON data, the file must be opened in 'r' mode.Final Answer:
open('data.json', 'r') -> Option AQuick Check:
Read mode = 'r' [OK]
- Using 'w' which overwrites the file
- Using 'a' which appends data
- Using 'x' which fails if file exists
What will be the output of the following Python code?
import json
with open('data.json', 'w') as f:
json.dump({'name': 'Alice', 'age': 30}, f)
with open('data.json', 'r') as f:
data = json.load(f)
print(data['age'])Solution
Step 1: Write dictionary to JSON file
The code writes {'name': 'Alice', 'age': 30} to 'data.json' using json.dump.Step 2: Read JSON file and access 'age'
json.load reads the file back as a dictionary, then data['age'] accesses the value 30.Final Answer:
30 -> Option BQuick Check:
data['age'] = 30 [OK]
- Expecting string output instead of integer
- Confusing json.dump and json.load usage
- Assuming file read fails without writing first
Identify the error in this code snippet that tries to read JSON data from a file:
import json
f = open('data.json', 'r')
data = json.load(f)
f.close()
print(data)Solution
Step 1: Check import and file open
The code imports json and opens the file in 'r' mode correctly.Step 2: Verify json.load and file close
json.load reads JSON data properly, and file is closed with f.close().Final Answer:
No error, code works correctly -> Option DQuick Check:
Code reads JSON file correctly [OK]
- Forgetting to import json
- Opening file in wrong mode
- Not closing the file after reading
You want to save a Python dictionary {'a': 1, 'b': 2, 'c': 3} to a JSON file and then read it back. Which code snippet correctly does this and prints the value of key 'b'?
Solution
Step 1: Write dictionary to JSON file correctly
json.dump writes Python dict to file opened in 'w' mode.import json with open('file.json', 'w') as f: json.dump({'a': 1, 'b': 2, 'c': 3}, f) with open('file.json', 'r') as f: data = json.load(f) print(data['b'])does this correctly.Step 2: Read JSON file and access key 'b'
json.load reads JSON back from file opened in 'r' mode, then data['b'] prints 2.Final Answer:
Code in Option A correctly saves and reads JSON, printing 2 -> Option AQuick Check:
json.dump to write, json.load to read [OK]
- Using json.load to write data
- Using json.dump to read data
- Opening files in wrong modes
