Last updated: Apr 11, 2024
Reading time·4 min
r+
modeTo update a JSON file in Python:
with open()
statement to open the file in reading mode.The code sample uses the following employees.json
starting file.
[ { "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.
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')
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.
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).
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.
with open(file_path, 'w', encoding='utf-8') as json_file: json.dump(employees_list, json_file) print('JSON file updated successfully')
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.
r+
modeYou 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.
[ { "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.
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')
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.
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.
# 👇️ 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.
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.
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.
[ { "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.
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')
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.
You can learn more about the related topics by checking out the following tutorials: