This video teaches three essential JSON file operations in Python: (1) Reading JSON files using json.load() with context managers, which automatically converts JSON types to Python types (e.g., null to None); (2) Modifying JSON data by appending new entries and writing back with json.dump() using indent for readability; (3) Safely accessing nested fields that may not exist in all records using the get() method to prevent KeyError exceptions. These skills form the foundation for working with JSON data in real-world applications.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
[Practice Problems] JSON in Python - Visually ExplainedAdded:
Have you had a chance to try our JSON and Python practice exercises? If you haven't, the notebook is linked in the description. You can pause here and give them a shot before watching us walk through the solutions in the rest of this video.
All right, let's jump into problem one.
In this problem, our goal is to read a file called library.json into Python and print each book's title, availability, and rating.
To get started, we create the library.json file manually and paste in the JSON content provided in the notebook.
Then, back in the notebook, in order to work with JSON in Python, we import the JSON module.
Then, we read library.json by using a context manager to open it in read mode.
If context managers are new to you, check out our video linked in the description.
Inside the context manager, we load the data using the load function.
The json.load function automatically converts JSON types into Python types.
So, our JSON array of objects will become a Python list of dictionaries.
Back in our code, we can store the list of dictionaries in a variable called books.
Okay, with our data loaded, we use a loop to output each book's information.
For each book, we check if available is true.
If it is, we set the status variable to available.
Otherwise, we set it to not available.
Then, we print each book's title, status, and rating using an f-string.
Running the cell, we see each book's information nicely formatted.
Notice that The Great Gatsby's rating prints as none.
That's because json.load automatically converts JSON's null type into Python's none type.
Great, let's move on to problem two.
Here, we'll load library.json, add a new book to the list, and write the updated list back to the file.
We start by opening library.json in read mode and using json.load to get our list of books.
To add a new entry to our books dictionary, we call books.append and pass a dictionary with the new books fields. title availability and rating Since this change only affects the Python dictionary and not the library.json file, we need to explicitly update our JSON file.
To do this, we open library.json in write mode and call json.dump passing in our books list and the file object.
We also pass in the optional argument indent equals two to keep the output human readable.
Running the cell we can open library.json to confirm that Pride and Prejudice now appears at the bottom of the list.
Perfect.
When working with JSON files, the read, modify, write pattern is really common and it's core to using JSON files for real data storage.
Great, let's move on to problem three.
In this problem, we need to modify library.json to have a new structure.
Some books with a genres field and one without.
Our goal is then to load the file and safely print each book's genres.
Rather than manually editing the file, we'll write the new content from Python so we know we're starting from exactly the right data.
To do this, we'll start by defining a Python string that holds our new JSON content.
Next, to write the content to library.json, we open the file in write mode and call the write function to replace whatever was in the file before.
Running this first cell, we can open library.json to confirm the new structure is there.
Four books with 1984 missing the genres field.
Great. Now back in the second cell we open library.json and load it into Python using the load function.
After loading the books, we loop over them.
For each book, we want to access genres, a nested list inside each book's dictionary.
But since our book, 1984, doesn't have a genres field, accessing it directly with square bracket syntax would throw a key error.
So, instead, we use the get method, which works the same as using square brackets when the key exists, but returns none when the key is missing.
To use this result later, we'll store it in a variable called genres.
Since the genres variable will hold either a list or the value none, we need to handle both cases.
If genres exists and has items, we print the title followed by the genres joined with a comma and space.
Otherwise, we print no genres listed.
Running the cell, we get the genres for three books and no genres listed for 1984, since that field wasn't in the JSON.
Using the get dictionary method when handling both cases is defensive programming in action.
Instead of assuming our data is well-formed, we take steps to handle any inconsistencies safely.
Great, and that wraps up all three practice exercises. If you want to keep building on your skills, check out our video on Python dictionaries. A lot of what you did in this video builds on top of dictionary fundamentals.
We're working on lots more Python explainer like this one, so be sure to subscribe so that you don't miss out.
If you have any questions or topics that you'd like to learn more about, let us know in the comments below. We'd love to hear from you. Thanks for watching.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsโข2026-05-28
How agent o11y differs from traditional o11y โ Phil Hetzel, Braintrust
aiDotEngineer
450 viewsโข2026-05-28
Re: ๐ฃ๏ธ๐theprophedu๐2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 viewsโข2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation๐ฏโ
LearnwithSahera
1K viewsโข2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsโข2026-05-29
Search Algorithms Explained in 60 Seconds! ๐ค๐จ
samarthtuliofficial
218 viewsโข2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsโข2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsโข2026-05-29











