DEV Community

Cover image for Working with JSON Files in Python
≀Paulo Portela
≀Paulo Portela

Posted on • Updated on

Working with JSON Files in Python

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
   }
]
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

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}]
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Book data written to books_data.json
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Modified book data written back to books_data.json
Enter fullscreen mode Exit fullscreen mode

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
   }
]
Enter fullscreen mode Exit fullscreen mode

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)