Python Tutorial: Working with JSON Data using the json Module
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use `json.loads` to parse JSON strings and `json.load` to parse JSON from open files into Python dictionaries/lists.
Briefing
JSON handling in Python hinges on two core moves: converting JSON strings/files into native Python objects with `json.loads`/`json.load`, and converting Python objects back into JSON with `json.dumps`/`json.dump`. Once that translation layer is in place, the rest is ordinary Python—indexing dictionaries, iterating lists, deleting keys, and formatting output for readability.
The tutorial starts with a multi-line JSON string that looks like a dictionary: a top-level object with a `people` key whose value is an array of person objects. Each person object contains fields like `name`, `phone`, `emails`, and `license` (booleans such as `true`/`false`, plus `null` values). Loading that string uses `json.loads`, producing a real Python `dict`. A conversion table is highlighted as the reason the data becomes usable: JSON objects become Python dictionaries, JSON arrays become Python lists, JSON strings stay strings, JSON integers and numbers map to Python numeric types, JSON `true`/`false` become Python `True`/`False`, and JSON `null` becomes Python `None`.
With the JSON now in Python form, the workflow becomes straightforward. Because `people` is a list, the code iterates through it, treats each element as a dictionary, and pulls out fields like `name`. The reverse direction uses `json.dumps` to serialize modified Python data back into a JSON string. A practical example removes phone numbers by deleting the `phone` key from each person dictionary, then dumps the result. Readability improvements matter in real work: `indent` formats nested structures with consistent spacing, and `sort_keys=True` alphabetizes keys so output is easier to scan and diff.
The tutorial then scales from strings to files. A local JSON file named `States.json` is loaded with `json.load` inside a `with open(...)` block, turning the file contents into Python objects. The structure again includes a top-level `states` key holding a list of state objects with fields like `name`, `abbreviation`, and `area_codes`. Iteration and field access mirror the earlier example, but now the data source is persistent storage. Writing back to disk uses `json.dump` with a `with open(..., 'w')` block. After deleting `area_codes` via `del`, the updated object is written to `new_States.json`, and `indent=2` makes the output human-readable.
Finally, the tutorial connects JSON parsing to real-world API usage. It fetches currency conversion data from a public Yahoo Finance API using `urllib.request.urlopen`, reads the response as a string, and loads it with `json.loads`. The resulting Python object contains a `list` with `meta` information (including a `count` of 188 items) and a `resources` section holding conversion entries. The code verifies the resource count, loops through each conversion to extract `name` and `price`, builds a lookup dictionary of USD rates, and demonstrates converting 50 US dollars into euros and Indian rupees by multiplying the input amount by the appropriate rate. The takeaway is practical: mastering JSON-to-Python translation makes it easy to ingest external data, transform it, and reuse it in repeatable scripts.
Cornell Notes
Python’s `json` module provides a direct pipeline for working with JSON data: load JSON into Python objects, manipulate them like normal dictionaries/lists, then dump them back to JSON. `json.loads` converts a JSON string to Python types (objects→dicts, arrays→lists, `true/false`→`True/False`, `null`→`None`). `json.dumps` serializes Python objects back into JSON strings, with options like `indent` for readability and `sort_keys` for consistent ordering. For files, `json.load` reads JSON from disk and `json.dump` writes updated objects back to JSON files. The same approach applies to API responses: fetch JSON, parse it, extract fields, and compute results such as currency conversions.
What exact type conversions happen when JSON is loaded into Python?
How does the tutorial access nested JSON fields after loading?
How do `json.dumps` options like `indent` and `sort_keys` change output?
What’s the difference between `json.load`/`json.loads` and `json.dump`/`json.dumps`?
How does the API example turn JSON currency data into usable conversion rates?
Review Questions
- When JSON contains `null`, what Python value appears after parsing, and why does that matter for downstream code?
- Which functions would you use to parse JSON from a file versus a string, and how do their outputs differ in usage?
- In the currency API example, what nested keys must be traversed to reach the conversion `name` and `price` fields?
Key Points
- 1
Use `json.loads` to parse JSON strings and `json.load` to parse JSON from open files into Python dictionaries/lists.
- 2
Expect JSON `true/false` to become Python `True/False` and JSON `null` to become Python `None` after parsing.
- 3
Treat loaded JSON like native Python: iterate lists, index dictionaries, and access nested fields directly.
- 4
Use `json.dumps`/`json.dump` to serialize Python objects back to JSON, and apply `indent` for readable output.
- 5
Delete unwanted fields by removing keys from dictionaries (e.g., `del person['phone']`) before dumping back to JSON.
- 6
When working with APIs, fetch the response, parse it with `json.loads`, then extract needed values by walking the nested JSON structure.
- 7
Build lookup dictionaries from parsed JSON (mapping conversion names to prices) to make repeated calculations fast and simple.