liveBook · Manning

Json.load

As explained in Python Workout, Second Edition

json.load is a Python function that parses a JSON document from a text file-like object and returns the corresponding Python object.

json.load reads a JSON document from a text file-like object and converts it into Python objects. Use it when the file contains one complete JSON value, such as an array; use json.loads for JSON text already held in a string. section 6.22 section 6.23

A practical workflow for processing JSON score files is:

  • Find the .json files in the target directory and open each file for reading. section 6.23
  • Call json.load on the file object; for an array of dictionaries, iterate through each dictionary and its subject-score pairs. section 6.23
  • Accumulate scores by subject, then calculate the minimum, maximum, and average for the report. section 6.23
  • Remember that valid JSON uses double quotes rather than Python-style single quotes. section 6.22
[{"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}
]

This sample JSON array shows the list-of-dictionaries structure returned by json.load for a class score file. section 6.22

Class score analysis

Loading 9a.json produces student dictionaries such as math: 90 and science: 97. Grouping those values by subject yields reports such as science: min 75, max 97, average 86.4. section 6.22 section 6.23

How Python Workout, Second Edition covers it

How deep it goes
Explains file-object parsing and list-of-dictionaries traversal, without examining parser internals.
Examples
Uses class score files containing student dictionaries and produces subject-level minimum, maximum, and average reports.
What it assumes
Assumes familiarity with files, lists, dictionaries, iteration, and basic statistics.
What you can do after
Apply it via Python’s json module to read complete JSON files and traverse their resulting Python structures.
How it connects
Connects JSON files with Python objects, file objects, json.loads, dictionaries, lists, and statistical aggregation.

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, Second Edition. The section links point to the parts of the book it draws on.