Skip to main content

How to Update JSON Files in Python

Updating JSON files is a common task when working with data storage and configuration.

This guide explores multiple methods for updating JSON files in Python, including reading, modifying, and writing data using json.load(), json.dump(), and with open() statements.

Updating JSON Files using Separate Read and Write Operations

This is the most common approach and involves first opening the file for reading, updating, and then writing to it by opening it again for writing.

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'] = 'Tom Nolan'

with open(file_path, 'w', encoding='utf-8') as json_file:
json.dump(employees_list, json_file)

print('JSON file updated successfully')
  • The first with open() statement opens the file in read mode ('r') using UTF-8 encoding and deserializes the JSON to a Python list or dictionary object using json.load().
  • The object is updated as needed.
  • A second with open() statement opens the same file, this time in write mode ('w'), and writes the modified object back to the file using json.dump().
  • The with statements handle automatically closing the files, ensuring that no memory leaks happen.

Updating JSON Files with Read/Write Mode (r+)

You can also update a JSON file by opening it in read/write mode ('r+'), allowing reading and writing in a single operation.

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'] = 'Tom Nolan'

json_file.seek(0)
json.dump(employees_list, json_file)
json_file.truncate()

print('JSON file updated successfully')
  • We open the JSON file using read and write mode r+.
  • The JSON data is loaded into Python object using json.load().
  • We make changes to the object.
  • The json_file.seek(0) method moves the cursor to the beginning of the file.
  • Then the changes are written to the file by serializing the Python object to a JSON formatted string and writing it to the file using json.dump().
  • The json_file.truncate() method removes any extra characters at the end, if the new data is smaller than the original one.
note

When opening the file in r+ mode, you must explicitly move the file pointer back to the beginning of the file (using seek(0)) before writing to the file, otherwise the write operation will start at the end of the content that was previously read. Additionally, if the updated content is shorter than the original content, then you must use the truncate() method to cut of any remaining content that was previously in the file.

Updating JSON Files using the open() function directly

It is also possible to update a JSON file using the open() function directly instead of the with statement, however, this approach is not recommended because you will be responsible for closing the file, which might lead to memory leaks if you forget to do so, or if an unhandled exception happens before you manually call the close() function on the file object.

import json
file_path = 'employees.json'

json_file = open(file_path, 'r', encoding='utf-8')
employees_list = json.load(json_file)
json_file.close()

employees_list[1]['name'] = 'Tom Nolan'

json_file = open(file_path, 'w', encoding='utf-8')
json.dump(employees_list, json_file)
json_file.close()
print('JSON file updated successfully')