liveBook · Manning

Json

As explained in Python Workout: 50 ten-minute exercises

The Python standard-library json module serializes Python objects to JSON and parses JSON into Python objects.

When a program needs to analyze data exchanged between systems, JSON provides a portable representation. Python's standard-library json module converts JSON into built-in data structures and converts Python data back into JSON-formatted strings. section 5.26 section 5.27

A practical file-processing pattern is:

  • Find matching files such as scores/*.json with glob. section 5.27
  • Open each file and use json.load when it contains one JSON array; use json.loads for JSON held in a string or line. section 5.27
  • Organize the resulting lists and dictionaries, then calculate and print summaries. section 5.26

Class score summaries

Given files such as 9a.json and 9b.json, read each array of subject-score dictionaries, group scores by subject, and print each class's minimum, maximum, and average. Remember that valid JSON uses double quotes, not Python-style single quotes. section 5.26 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}
]

A JSON array of dictionaries illustrates the structure returned as Python data for score analysis. section 5.26

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains load, loads, dump, file selection, and nested data organization through a multi-file statistics exercise.
Examples
Uses class score files containing subject dictionaries and demonstrates minimum, maximum, and average summaries.
What it assumes
Assumes Python file handling and built-in collections while clarifying JSON's distinction from Python data types.
What you can do after
Apply it via json.load and json.loads to read JSON, organize records, and analyze multiple files.
How it connects
Connects JSON with Python data structures, text files, glob, XML, and CSV.

Also explained in

Compare how 4 books explain json →

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.