Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on • Edited 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 thejson module in Python to read JSON files.
  • Use thejson.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 modeExit fullscreen mode
importjson# Read book data from a JSON filewithopen(file="books_data.json",mode="r")asfile:books_data=json.load(fp=file)print(books_data)
Enter fullscreen modeExit 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 modeExit fullscreen mode

Writing JSON Files

  • Use thejson.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")
Enter fullscreen modeExit fullscreen mode

Output:

Book data written to books_data.json
Enter fullscreen modeExit 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

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

Output:

Modified book data written back to books_data.json
Enter fullscreen modeExit 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 modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Portugal
  • Education
    ISEP
  • Work
    Senior Software Developer @ adidas
  • Joined

More from≀Paulo Portela

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp