JSON is a text-based data representation used to exchange structured values between programs. Python's json module converts JSON-formatted strings or files into built-in data structures and converts Python data back into JSON. section 6.22 section 6.23
Process JSON-backed data with a small, implementation-focused workflow:
- Open a JSON file and use json.load to read a complete JSON array or object; use json.loads when the input is already a string. section 6.23
- Work with the resulting lists and dictionaries instead of immediately defining custom classes. section 6.23
- Remember that valid JSON uses double quotes, not Python's single-quoted string representation. section 6.22
Summarizing class scores
A scores directory can contain one JSON file per class. Iterate over glob('scores/*.json'), load each file, collect subject scores in a nested dictionary such as scores[filename][subject], and print each subject's minimum, maximum, and average. section 6.22 section 6.23
[{"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 a representative JSON array whose objects contain math, literature, and science scores. section 6.22


































