How to Append Data to a JSON File in Python? [+Video]

Problem Formulation

Given a JSON object stored in a file named "your_file.json" such as a list of dictionaries.

πŸ’¬ How to append data such as a new dictionary to it?

# File "your_file.json" (BEFORE)
[{"alice": 24, "bob": 27}]

# New entry:
{"carl": 33}

# File "your_file.json" (AFTER)
[{"alice": 24, "bob": 27}, {"carl": 33}]

Method 1: Using json.load(file) and json.dump(data, file)

To update a JSON object in a file, import the json library, read the file with json.load(file), add the new entry to the list or dictionary data structure data, and write the updated JSON object with json.dump(data, file).

In particular, here are the four specific steps to update an existing JSON file with a new entry:

  1. Import the json library with import json
  2. Read the JSON file in a data structure using data = json.load(file) after opening the file with open(filename, "r") in reading mode "r". At this point, you have the JSON data in your Python code as a dictionary or list (depending on how your data looks like—see conversion table below).
  3. Update the Python data structure with the new entry (e.g., a new dictionary to append to the list).
  4. Write the updated JSON data back to the JSON file using json.dump(data, file) after opening the file in writing mode using open(filename, "w").
import json

filename = 'your_file.json'
entry = {'carl': 33}

# 1. Read file contents
with open(filename, "r") as file:
    data = json.load(file)

# 2. Update json object
data.append(entry)

# 3. Write json file
with open(filename, "w") as file:
    json.dump(data, file)

In fact, when using the json.load() method, the returned Python object is converted from the JSON format using this conversion table:

JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

To get a better feeling for the most important data structures such as the Python dictionary, you may want to check out the Finxter Computer Science Academy:

The Ultimate Guide to Dictionaries in Python

The course is free for all Finxter premium members.

Method 2: Opening the File Only Once by Resetting the File Pointer

Instead of opening the file object twice, you can open it only once and reset the file pointer using file.seek(0) to overwrite the existing file content using these four steps:

  1. Use open("your_file.json", "r+") to create a file object in reading and writing mode "r+".
  2. Call json.load(file) to load the data from the file in your Python code. Now, you can update the data in your Python code. For example, if you JSON file is structured as a list of dictionaries, simply append a new dictionary.
  3. Use file.seek(0) to reset the file pointer to position 0, so you can overwrite it instead of appending the whole JSON object again.
  4. Call json.dump(data, file) to overwrite file with data.

Here’s the concrete code:

import json

filename = 'your_file.json'
# Old JSON File:
# [{"alice": 24, "bob": 27}]

entry = {'carl': 33}

with open(filename, "r+") as file:
    data = json.load(file)
    data.append(entry)
    file.seek(0)
    json.dump(data, file)

# New JSON file: 
# [{"alice": 24, "bob": 27}, {"carl": 33}]

Method 3: Creating an Initial JSON File and Updating It Subsequently

If you don’t yet have a JSON file, you can first create the file from an initial list as follows:

import json

filename = 'your_file.json'
lst = [{'alice': 24, 'bob': 27}]

# Write the initial json object (list of dicts)
with open(filename, mode='w') as f:
    json.dump(lst, f)

# Append the new dict to the list and overwrite whole file
with open(filename, mode='w') as f:
    lst.append({'carl':33})
    json.dump(lst, f)

Summary and Discussion

All three methods presented in this article use basically the same idea: calling json.dump(data, file) to update a file with some data. The data variable can hold all different JSON formats such as a list of dictionaries or a dictionary.

If you first need to load the data from the file, use json.load(file) on the file object opened in reading mode.

Generally, appending data to a JSON file is not a very good idea because, for each small update, you have to read and parse the whole file object. If your JSON file has n entries, the runtime complexity of just updating it is O(n).

A better approach would be to store the data as a CSV file that can be read line-by-line that simplifies parsing and updating significantly by just appending a single line to the file that has constant runtime complexity.

Thanks for studying this article, feel free to check out my free Python cheat sheet course to stay sharp and improve your Python skills continuously, day by day. πŸ™‚

You can sign up for free here:

Programmer Humor

“Real programmers set the universal constants at the start such that the universe evolves to contain the disk with the data they want.”xkcd