JSON Files in Python
Home Education How to Read and Write JSON Files in Python

How to Read and Write JSON Files in Python

by Williami

JSON (JavaScript Object Notation) is a lightweight and widely used format for exchanging data, especially in web applications and APIs. Python makes handling JSON straightforward through its built-in json module. In this guide, you’ll learn how to read from, write to, and manipulate JSON files in Python with simple, practical examples.

Reading JSON Files in Python

To read a JSON file and convert its contents into a Python object, follow these steps:

Steps:

  • Import the json module.
  • Use Python’s open() function with mode 'r' to read the file.
  • Pass the file object to json.load() to convert it into a Python dictionary.
JSON Files in Python

Example:

pythonCopyEditimport json
# Open and load the JSON file
with open('data.json', 'r') as f:
    data = json.load(f)

# Display the data as a Python dictionary
print(data)

Writing JSON Files in Python

Writing data to a JSON file is just as simple:

Steps:

  • Define your data as a Python dictionary.
  • Open a file in write mode ('w').
  • Use json.dump() to write the data to the file.

Example:

pythonCopyEditimport json
# Define data
data = {
    'name': 'Bob',
    'age': 25,
    'city': 'Los Angeles'
}

# Write to a JSON file
with open('output.json', 'w') as f:
    json.dump(data, f)

Modifying and Updating JSON Data

You can read an existing JSON file, update its contents, and save the changes to a new file.

Example:

pythonCopyEditimport json
# Read the existing file
with open('output.json', 'r') as f:
    output_data = json.load(f)

print("Original Data:", output_data)

# Modify the data
output_data['job'] = 'Engineer'

# Save to a new file
with open('modified_output.json', 'w') as f:
    json.dump(output_data, f)

# Read and print the modified file
with open('modified_output.json', 'r') as f:
    modified_data = json.load(f)

print("Modified Data:", modified_data)

Explanation:

  • output.json is read and parsed into a dictionary.
  • A new key-value pair ('job': 'Engineer') is added.
  • The updated dictionary is saved to modified_output.json.
  • The new file is then read and printed to confirm the update.

Frequently Asked Questions

What is JSON used for in Python?

JSON is commonly used to store and transfer data between applications, especially in APIs and web services.

Can I convert a Python dictionary to a JSON string?

Yes, you can use json.dumps() to convert a dictionary to a JSON-formatted string.

What is the difference between json.dump() and json.dumps()?

  • json.dump() writes JSON data directly to a file.
  • json.dumps() returns a JSON string from a Python object.

How do I format JSON output for readability?

Use the indent parameter in json.dump() or json.dumps(). Example: json.dump(data, f, indent=4).

Conclusion

Working with JSON in Python is efficient and intuitive thanks to the json module. Whether you are dealing with API responses or configuration files, you can seamlessly read, write, and update JSON files using simple code constructs.

By mastering these techniques, you’ll be well-prepared to handle structured data in your Python projects.

You may also like

Leave a Comment