0
0
Pythonprogramming~10 mins

Working with JSON files in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a JSON file named 'data.json' for reading.

Python
with open('data.json', [1]) as file:
    data = file.read()
Drag options to blanks, or click blank then click option'
A'w'
B'r'
C'a'
D'x'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' instead of 'r' will overwrite the file.
Using 'a' opens the file for appending, not reading.
2fill in blank
medium

Complete the code to load JSON data from a file object named 'file'.

Python
import json

data = json.[1](file)
Drag options to blanks, or click blank then click option'
Aload
Bdump
Cloads
Ddumps
Attempts:
3 left
💡 Hint
Common Mistakes
Using dump or dumps which are for writing JSON.
Using loads which expects a string, not a file.
3fill in blank
hard

Fix the error in the code to write a Python dictionary 'data' to a JSON file named 'output.json'.

Python
import json

with open('output.json', 'w') as file:
    json.[1](data, file)
Drag options to blanks, or click blank then click option'
Adump
Bloads
Cload
Ddumps
Attempts:
3 left
💡 Hint
Common Mistakes
Using load or loads which are for reading JSON.
Using dumps without writing the string to the file.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length, but only for words longer than 3 characters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Comparing the word string directly to 3, which is invalid.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that converts keys to uppercase, keeps values, and filters only items where value is greater than 10.

Python
data = {'a': 5, 'b': 15, 'c': 20}
result = { [1]: [2] for k, v in data.items() if [3] }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Cv > 10
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original key without converting to uppercase.
Filtering with incorrect conditions like k > 10.