How to update a JSON file in Python [3 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. How to update a JSON file in Python
  2. Update a JSON file by opening it in r+ mode
  3. Updating a JSON file in Python by opening it directly

# How to update a JSON file in Python

To update a JSON file in Python:

  1. Use the with open() statement to open the file in reading mode.
  2. Read the file's contents.
  3. Update the JSON list or dictionary.
  4. Open the file in writing mode and write the updated value to the file.

The code sample uses the following employees.json starting file.

employees.json
[ { "id": 1, "name": "Alice", "languages": ["Java", "JavaScript"] }, { "id": 2, "name": "Bobby", "languages": ["Python", "JavaScript"] }, { "id": 3, "name": "Carl", "languages": ["Typescript", "JavaScript"] } ]

And here is the related Python code.

main.py
import json file_path = 'employees.json' with open(file_path, 'r', encoding='utf-8') as json_file: employees_list = json.load(json_file) print(employees_list) employees_list[1]['name'] = 'Bobby Hadz' print(employees_list[1]) with open(file_path, 'w', encoding='utf-8') as json_file: json.dump(employees_list, json_file) print('JSON file updated successfully')

update json file in python

The code for this article is available on GitHub

We used the with open() statement to open the .json file in r (reading) mode.

The json.load() method is used to deserialize a file to a Python object.

main.py
import json file_path = 'employees.json' with open(file_path, 'r', encoding='utf-8') as json_file: employees_list = json.load(json_file)

Note that we used json.load() and not json.loads().

The json.loads() method is used to deserialize a JSON string (not a file) to a Python object.

The next step is to update the values in the list or dictionary (depending on your file's contents).

main.py
print(employees_list) employees_list[1]['name'] = 'Bobby Hadz' print(employees_list[1])

Once the values in the object are updated, you have to open the file in w (writing) mode.

main.py
with open(file_path, 'w', encoding='utf-8') as json_file: json.dump(employees_list, json_file) print('JSON file updated successfully')
The code for this article is available on GitHub

The json.dump() method serializes the supplied object as a JSON formatted stream and writes it to a file.

Notice that we used json.dump() and not json.dumps().

The json.dumps() method converts a Python object (not a file) to a JSON formatted string.

After issuing the python main.py command, I can see that the JSON file has been updated successfully.

json file updated successfully

# Update a JSON file by opening it in r+ mode

You can also update a JSON file by opening it in r+ (reading and writing) mode.

This should be a bit more performant because you only have to open the file once.

The code sample uses the following employees.json starting file.

employees.json
[ { "id": 1, "name": "Alice", "languages": ["Java", "JavaScript"] }, { "id": 2, "name": "Bobby", "languages": ["Python", "JavaScript"] }, { "id": 3, "name": "Carl", "languages": ["Typescript", "JavaScript"] } ]

And here is the related Python code.

main.py
import json file_path = 'employees.json' with open(file_path, 'r+', encoding='utf-8') as json_file: employees_list = json.load(json_file) employees_list[1]['name'] = 'Bobby Hadz' print(employees_list[1]) # 👇️ place the cursor at the beginning of the file json_file.seek(0) json.dump(employees_list, json_file) json_file.truncate() print('JSON file updated successfully')

update json file by opening it in reading plus mode

The code for this article is available on GitHub

The r+ mode opens the file in reading and writing mode.

We first convert the JSON file to a native Python object using json.load().

The next step is to update the Python object.

main.py
with open(file_path, 'r+', encoding='utf-8') as json_file: employees_list = json.load(json_file) employees_list[1]['name'] = 'Bobby Hadz' print(employees_list[1])

However, at this point, we've read the contents of the file, so the cursor is placed at the end.

We have to use the file.seek() method to place the cursor at the beginning of the file.

main.py
# 👇️ place the cursor at the beginning of the file json_file.seek(0)

Once the cursor is at the beginning, we write the updated Python object to the file.

main.py
json.dump(employees_list, json_file) json_file.truncate()

The file.truncate() method is used to cover the case where the updated contents of the file are fewer bytes.

# Updating a JSON file in Python by opening it directly

The previous examples used the with statement to open the file, however, you can also use the open() function directly.

The code sample uses the same employees.json starting file.

employees.json
[ { "id": 1, "name": "Alice", "languages": ["Java", "JavaScript"] }, { "id": 2, "name": "Bobby", "languages": ["Python", "JavaScript"] }, { "id": 3, "name": "Carl", "languages": ["Typescript", "JavaScript"] } ]

And here is the related Python code.

main.py
import json file_path = 'employees.json' json_file = open(file_path, 'r', encoding='utf-8') employees_list = json.load(json_file) json_file.close() print(employees_list) employees_list[1]['name'] = 'Bobby Hadz' print(employees_list[1]) json_file = open(file_path, 'w', encoding='utf-8') json.dump(employees_list, json_file) json_file.close() print('JSON file updated successfully')

update json file by opening it directly

The code for this article is available on GitHub

We used the open() function directly in the code sample.

When using the with open() statement, Python automatically takes care of closing the file, even if an error is raised.

When you use the open() function directly, you have to manually close the file.

If you forget to close the file after you're done reading/writing, you'll get a memory leak.

I've also written an article on how to merge JSON files in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.