Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What Python module is used to work with JSON files?
The json module is used to read from and write to JSON files in Python.
Click to reveal answer
beginner
How do you read JSON data from a file in Python?
Use json.load(file_object) after opening the file in read mode.
Click to reveal answer
beginner
How do you write Python data to a JSON file?
Use json.dump(data, file_object) after opening the file in write mode.
Click to reveal answer
intermediate
What is the difference between json.load() and json.loads()?
json.load() reads JSON data from a file object, while json.loads() reads JSON data from a string.
Click to reveal answer
beginner
Why is it important to open JSON files with the correct mode?
Opening a file in the wrong mode (e.g., reading in write mode) causes errors. Use 'r' for reading and 'w' for writing to handle JSON files properly.
Click to reveal answer
Which function reads JSON data from a file in Python?
Ajson.dumps()
Bjson.loads()
Cjson.dump()
Djson.load()
✗ Incorrect
json.load() reads JSON data from a file object.
Which mode should you use to open a JSON file for writing?
A'w'
B'r'
C'a'
D'x'
✗ Incorrect
Use 'w' mode to write to a file, which overwrites existing content.
What does json.dumps() do?
AConverts Python data to a JSON string
BReads JSON from a file
CWrites JSON to a file
DParses JSON string to Python data
✗ Incorrect
json.dumps() converts Python objects into JSON formatted strings.
If you have a JSON string, which function converts it to Python data?
Ajson.load()
Bjson.dumps()
Cjson.loads()
Djson.dump()
✗ Incorrect
json.loads() parses a JSON string into Python data.
What happens if you try to read a JSON file opened in write mode?
AIt reads the file successfully
BIt raises an error
CIt creates a new file
DIt appends data
✗ Incorrect
Opening a file in write mode and trying to read causes an error because the file is not readable in that mode.
Explain the steps to read JSON data from a file in Python.
Think about how you open files and use the json module.
You got /3 concepts.
Describe how to save Python data as JSON in a file.
Remember to convert Python data to JSON format and write it.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of using JSON files in Python programs?
easy
A. To create graphical user interfaces
B. To execute Python code faster
C. To save and load data in a simple text format
D. To compile Python programs into executables
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 C
Quick Check:
JSON files = data storage [OK]
Hint: JSON files store data simply as text [OK]
Common Mistakes:
Thinking JSON speeds up code execution
Confusing JSON with GUI creation
Assuming JSON compiles code
2.
Which of the following is the correct way to open a JSON file named data.json for reading in Python?
easy
A. open('data.json', 'r')
B. open('data.json', 'w')
C. open('data.json', 'a')
D. open('data.json', 'x')
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 A
Quick Check:
Read mode = 'r' [OK]
Hint: Use 'r' mode to read files [OK]
Common Mistakes:
Using 'w' which overwrites the file
Using 'a' which appends data
Using 'x' which fails if file exists
3.
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'])
medium
A. Alice
B. 30
C. {'name': 'Alice', 'age': 30}
D. Error: file not found
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 B
Quick Check:
data['age'] = 30 [OK]
Hint: json.load returns Python dict from JSON file [OK]
Common Mistakes:
Expecting string output instead of integer
Confusing json.dump and json.load usage
Assuming file read fails without writing first
4.
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)
medium
A. Missing import statement for json
B. File not opened in write mode
C. File not closed properly
D. No error, code works correctly
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 D
Quick Check:
Code reads JSON file correctly [OK]
Hint: json.load needs file opened in 'r' mode [OK]
Common Mistakes:
Forgetting to import json
Opening file in wrong mode
Not closing the file after reading
5.
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'?
hard
A.
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'])
B.
import json
with open('file.json', 'r') as f:
json.dump({'a': 1, 'b': 2, 'c': 3}, f)
with open('file.json', 'w') as f:
data = json.load(f)
print(data['b'])
C.
import json
f = open('file.json', 'w')
json.load({'a': 1, 'b': 2, 'c': 3}, f)
f.close()
f = open('file.json', 'r')
data = json.dump(f)
f.close()
print(data['b'])
D.
import json
with open('file.json', 'w') as f:
json.load({'a': 1, 'b': 2, 'c': 3}, f)
with open('file.json', 'r') as f:
data = json.dump(f)
print(data['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 A
Quick Check:
json.dump to write, json.load to read [OK]
Hint: Use json.dump to write, json.load to read JSON files [OK]