When a JSON file contains one complete array, use json.load() to read the file object and deserialize its contents into Python objects. Unlike json.loads(), which parses a JSON string, json.load() works directly with the open file. section 5.26 section 5.27
A practical file-processing pattern is:
- Open each .json file, then pass its file object to json.load(). section 5.27
- Iterate over the returned list of dictionaries and reorganize student-level records into subject-level score lists. section 5.27
- Calculate each subject’s minimum, maximum, and average, while remembering that valid JSON uses double quotes. section 5.26
Read a class score file
import json with open("scores/9a.json") as infile: results = json.load(infile) for result in results: for subject, score in result.items(): print(subject, score) Here, results is a list of dictionaries: each dictionary contains one student’s subject scores. section 5.27
[{"math" : 90, "literature" : 98, "science" : 97},
{"math" : 65, "literature" : 79, "science" : 85},
{"math" : 78, "literature" : 83, "science" : 75},
{"math" : 92, "literature" : 78, "science" : 85},
{"math" : 100, "literature" : 80, "science" : 90}
]
Inspect the JSON array that json.load() converts into Python records. section 5.26


