How to read and write JSON files in Python
Tags: programming, python, tutorial, beginners
Published: 2024-03-19 10:00:00

## Using Python to Read, Write, and Manipulate JSON Files

JSON (JavaScript Object Notation) is a popular data interchange format that is easy for humans to read and write. JSON often comes into play when interacting with web APIs or HTTP requests within the field of programming. Python provides built-in support for working with JSON files through the `json` module. In this article, we will discuss how to use Python to read, write, and manipulate JSON files.

## Reading JSON Files

To read a JSON file in Python, you can follow these steps:

1. Import the `json` module.
2. Open the JSON file using Python's `open()` function with the mode set to `r`.
3. Use the `json.load()` function to load the contents of the file as a Python dictionary.

Here is an example code snippet that demonstrates how to read a JSON file:

```python
import json

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

# Print the data (it will be stored as a Python dictionary)
print(data)
```

## Writing JSON Files

To write data to a JSON file in Python, you can use the `json` module as well. Here are the steps to write data to a JSON file:

1. Define the data that you want to write as a Python dictionary.
2. Open a new file using Python's `open()` function.
3. Use the `json.dump()` function to write the dictionary data to the file in JSON format.

Here is an example code snippet that demonstrates how to write data to a JSON file:

```python
import json

# Define the data as a Python dictionary
data = {
    'name': 'Bob',
    'age': '25',
    'city': 'Los Angeles'
}

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

```

You can read the `output.json` file, modify the data dictionary by adding a new key-value pair, and then save it as another JSON file. Here's an example code snippet to achieve that:

```python
# Read the data from the written file and print it
with open('output.json') as f:
    output_data = json.load(f)
    print(output_data)

# Modify the data by adding a new key-value pair
output_data['job'] = 'Engineer'

# Write the modified data to a new JSON file
with open('modified_output.json', 'w') as f:
    json.dump(output_data, f)
    
# Read the modified data from the newly written file and print it
with open('modified_output.json') as f:
    modified_data = json.load(f)
    print('Modified data:', modified_data)
```

In this code snippet:

- We first read the data from the `output.json` file.
- We then modify the `output_data` dictionary by adding a new key-value pair.
- The modified data is then written to a new JSON file named `modified_output.json`.
- Finally, we read the modified data from `modified_output.json` and print it.

## Conclusion

In conclusion, Python's `json` module makes it simple to work with JSON files. You can read, write, and manipulate JSON data using Python's built-in functions, making it a powerful tool for handling JSON data in your Python projects.

---

## Explore more

{% embed https://dev.to/luca1iu %}


Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

{% cta https://www.linkedin.com/in/lucaliu-data %} 🚀 Connect with me on LinkedIn {% endcta %}

{% cta https://twitter.com/Luca_DataTeam %} 🎃 Connect with me on X {% endcta %}


{% cta https://www.instagram.com/luca1iu?igsh=MmJneDFpdjNtYWU1&utm_source=qr %} 🌍 Connect with me on Instagram {% endcta %}