liveBook · Manning

Json.load

As explained in Python Workout: 50 ten-minute exercises

json.load() is a Python function that reads and parses JSON from a file-like object into the corresponding Python value.

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

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains file-object deserialization and returned list iteration, while deferring detailed implementation internals.
Examples
Uses high-school class score files containing JSON arrays of subject-score dictionaries.
What it assumes
Assumes familiarity with opening files, lists, dictionaries, and iteration; distinguishes file parsing from string parsing.
What you can do after
Apply it via Python’s json module to load structured records and compute subject-level statistics.
How it connects
Connects JSON files with Python objects, json.loads(), dictionaries, and file handling.

Also explained in

Compare how 3 books explain json.load →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.