liveBook · Manning

JSON

As explained in Python Workout, Second Edition

JSON is a text-based data-interchange format that represents structured values such as objects, arrays, strings, numbers, booleans, and null.

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

How Python Workout, Second Edition covers it

How deep it goes
Explains parsing, serialization, file traversal, nested dictionaries, and a statistics exercise, but not JSON internals.
Examples
Uses class-score arrays, nested score dictionaries, CSV-to-JSON conversion, and directory metadata stored as JSON.
What it assumes
Assumes Python file handling, iteration, lists, and dictionaries while treating JSON-to-Python conversion explicitly.
What you can do after
Build Python programs that read, transform, analyze, and write JSON-formatted data.
How it connects
Connects JSON with Python, the json module, files, dictionaries, XML, CSV, and glob.

Also explained in

Compare how 35 books explain JSON →

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.