
Introduction
JSON (JavaScript Object Notation) is a flexible format for storing data, including information about multiple books. In this chapter, we'll explore how to work with JSON files in Python, focusing on using multiple books as examples.
Topics
- Reading JSON Files
- Writing JSON Files
- Manipulating JSON Data
Reading JSON Files
- Use the
json
module in Python to read JSON files. - Use the
json.load()
function to load JSON data from a file into a Python dictionary.
Example
JSON file named books_data.json:
[{"title":"Python Programming","author":"John Smith","pages":300},{"title":"Data Science Essentials","author":"Alice Johnson","pages":250},{"title":"Web Development Basics","author":"Michael Brown","pages":200}]
importjson# Read book data from a JSON filewithopen(file="books_data.json",mode="r")asfile:books_data=json.load(fp=file)print(books_data)
Output:
[{'title': 'Python Programming', 'author': 'John Smith', 'pages': 300}, {'title': 'Data Science Essentials', 'author': 'Alice Johnson', 'pages': 250}, {'title': 'Web Development Basics', 'author': 'Michael Brown', 'pages': 200}]
Writing JSON Files
- Use the
json.dump()
function to write Python data to a JSON file. - Ensure the data you're writing is serializable to JSON format.
Example
importjson# Book data for multiple booksbooks=[{"title":"Python Programming","author":"John Smith","pages":300},{"title":"Data Science Essentials","author":"Alice Johnson","pages":250},{"title":"Web Development Basics","author":"Michael Brown","pages":200}]# Write book data to a JSON filewithopen(file="books_data.json",mode="w")asfile:json.dump(obj=books,fp=file)print("Book data written to books_data.json")
Output:
Book data written to books_data.json
Manipulating JSON Data
- Use Python's dictionary methods to manipulate JSON data once it's loaded.
- Convert JSON data to Python objects (dict, list, etc.) for easy manipulation.
Example
importjson# Read book data from a JSON filewithopen(file="books_data.json",mode="r")asfile:books_data=json.load(file)# Manipulate book dataforbookinbooks_data:book["pages"]+=50# Increase the number of pages by 50 for each book# Write modified book data back to the filewithopen(file="books_data.json",mode="w")asfile:json.dump(books_data,file)print("Modified book data written back to books_data.json")
Output:
Modified book data written back to books_data.json
Output file for modified books_data.json:
[{"title":"Python Programming","author":"John Smith","pages":350},{"title":"Data Science Essentials","author":"Alice Johnson","pages":300},{"title":"Web Development Basics","author":"Michael Brown","pages":250}]
Conclusion
JSON files offer a convenient way to store and manage data for multiple books. Python'sjson
module provides powerful tools for reading, writing, and manipulating JSON files, making it suitable for handling server configurations involving various resources such as books and beyond.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse