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
}
]
import json
# Read book data from a JSON file
with open(file="books_data.json", mode="r") as file:
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
import json
# Book data for multiple books
books = [
{"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 file
with open(file="books_data.json", mode="w") as file:
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
import json
# Read book data from a JSON file
with open(file="books_data.json", mode="r") as file:
books_data = json.load(file)
# Manipulate book data
for book in books_data:
book["pages"] += 50 # Increase the number of pages by 50 for each book
# Write modified book data back to the file
with open(file="books_data.json", mode="w") as file:
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's json
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)