The issue here is that you've opened a file and read its contents so the cursor is at the end of the file. By writing to the same file handle, you're essentially appending to the file.

The easiest solution would be to close the file after you've read it in, then reopen it for writing.

with open("replayScript.json", "r") as jsonFile:
    data = json.load(jsonFile)

data["location"] = "NewPath"

with open("replayScript.json", "w") as jsonFile:
    json.dump(data, jsonFile)

Alternatively, you can use seek() to move the cursor back to the beginning of the file then start writing, followed by a truncate() to deal with the case where the new data is smaller than the previous.

with open("replayScript.json", "r+") as jsonFile:
    data = json.load(jsonFile)

    data["location"] = "NewPath"

    jsonFile.seek(0)  # rewind
    json.dump(data, jsonFile)
    jsonFile.truncate()
Answer from Shawn Chin on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › need help understanding how to write a file to json (python) without overwriting it
r/learnpython on Reddit: need help understanding how to write a file to json (python) without overwriting it
October 4, 2022 -
My Code:  Python, (Using discord rewrite if this matters)

user_id = str(ctx.message.author.id)
        print(user_id)

        # reads data
        with open("test.json", "r") as f:
            data = json.load(f)
            print(data)
            print(type(data))

            # checks if user is in data
            if user_id not in data['users'].__str__():
                data.update({})
                print(data)

                # checks if data is type list
                if type(data) is dict:
                    data = [data]
                    print(type(data))

                # updates data with new user
                data.append({"user_id": user_id})
                print(data)
                
                # writes new data to json file
                with open("test.json", "w"):
                    json.dump(data, f)

Error Code:  Command raised an exception: UnsupportedOperation: not writable
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
July 21, 2026 - Instead of using --indent here, ... JSON file as the infile and another JSON file as the outfile. If the target JSON file exists, then you overwrite its contents....
🌐
GeeksforGeeks
geeksforgeeks.org › python › modify-json-fields-using-python
Modify Json Fields Using Python - GeeksforGeeks
July 23, 2025 - In this example, below code parses a sample JSON data string representing a person's information, changes the 'name' field from "Eva" to "Sophia," and then converts the modified data back to JSON format. The resulting JSON, with the updated name, is printed to the console. Python3
🌐
Reddit
reddit.com › r/learnpython › can we overwrite json data in python subject to user inputs?
r/learnpython on Reddit: can we overwrite json data in python subject to user inputs?
January 13, 2022 -

Hey all, I am learning about json in python and want to better understand how to edit json files, I understand how to add more data at the end of the file but i can't find anything online about how to edit the data in the file, what i want to do is change the values of things that are already put into the file. for example the file may have 'cat': 1.00 and i want to change the 1.00 to a user specified input can someone recommend some resources and videos that can help me with this as all i find is about adding data to the end. (P.s This is not a easily searchable question as ive looked on many sites like https://www.w3schools.com/python/python_json.asp and https://docs.python.org/3/library/json.html and https://realpython.com/python-json/ and there is nothing about overwriting data)

🌐
Reddit
reddit.com › r/learnpython › what's the quickest way to replace json data?
r/learnpython on Reddit: What's the quickest way to replace json data?
December 29, 2021 -

I'm trying to read through documentation, but I haven't found a direct way to do it. So Reddit we go.

I'm trying to replace a list of json data with a dictionary, because it makes it easier to do SimpleNamespace(**d) on everything. I have the dictionary, but how do I replace the data? I never really learned file IO, especially with json data.

🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-modify-json-file-in-pythonexample.html
How to Modify JSON File in Python? - ItSolutionstuff.com
October 30, 2023 - In this example, you will learn python read and write to same json file. We will look at an example of append json file in python. We will use python open json file and modify. I would like to show you how to update json file in python.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 49711250 › overwrite-existing-json-using-python
Overwrite existing JSON using python - Stack Overflow
import json import os import csv os.chdir('C:\\path\\to\\folder') # it is the path of *.JSON file csv_file=open('Export_C.csv') csv_reader=csv.reader(csv_file, delimiter=',') next(csv_reader) for row in csv_reader: a=row[3] # 'a' stores the numerical data from csv file with open('add_location.json') as json_data: data = json.load(json_data) for Occupancies in data['Valuation']['Occupancies']: Code=data['Valuation']['Occupancies'][0]['Code'] Height=data['Valuation']['Occupancies'][0]['Height'] print(Code, Height) ... How do I overwrite the 'Code' value in JSON with the value stored in 'a'? I want to overwrite JSON and save it with the new value.
🌐
Reddit
reddit.com › r/learnpython › appending data to json txt file without overwriting
r/learnpython on Reddit: Appending data to JSON txt file without overwriting
January 19, 2019 -

I’m trying to write data to a txt file. The current script works but it overwrites the data within the txt file every time the script is run. Any help would be highly appreciated.

import json

data = {}  
data['people'] = []

data['people'].append({  
    'name': 'Scott',
    'website': 'stackabuse.com',
    'from': 'Nebraska'
})
data['people'].append({  
    'name': 'Larry',
    'website': 'google.com',
    'from': 'Michigan'
})
data['people'].append({  
    'name': 'Tim',
    'website': 'apple.com',
    'from': 'Alabama'
})

with open('d.txt', 'w') as outfile:
    json.dump(data, outfile)

with open('d.txt') as json_file:  
    data = json.load(json_file)
    for p in data['people']:
        print('Name: ' + p['name'])
        print('Website: ' + p['website'])
        print('From: ' + p['from'])
        print('')
🌐
Like Geeks
likegeeks.com › home › python › json manipulation and conversion techniques in python
JSON Manipulation and Conversion Techniques in Python
October 15, 2023 - Here we pass two arguments to the function json.dumps(). The first one ‘data’ contains the JSON object that we stored in a Python variable. The second is the sort_keys argument, that when set to True, sorts the data alphabetically and returns the JSON object as a string. The following code uses this functionality: ... import json file = open('jsonData.json', 'r') data = json.load(file) file.close() print(json.dumps(data, sort_keys=True))
🌐
Reddit
reddit.com › r/learnpython › appending/overwriting a json file
r/learnpython on Reddit: Appending/Overwriting a JSON file
February 14, 2020 - Hi all, so I posted a few days ago about appending to a .json file and I've made progress, but still not getting the desired results. Basically I…
🌐
Stack Overflow
stackoverflow.com › questions › 65302457 › how-to-overwrite-with-json-dump
python 3.x - How to overwrite with Json.dump? - Stack Overflow
EDIT: The overwriting works successfully when this condition (if monitor_Data['monitor_name'] == k['name'] and k['id'] == m['id'] \ and k['modified'] < modification_date(file_Path):) is taken out. However, once I put it back in content is appended upon multiple execution. for k in listOfMonitors: for m in monitor_Name_And_Mod_Date_List: if k['id'] == m['id'] and monitor_Data['monitor_name'] == m['name']: print("test") print("Value: ", k['id']) if k['id'] not in monitor_Data: # NEED TO FIX UPDATE CONDITION print("attempting update") monitor_Data.update(k) # print("updated") # print(json.dumps(m
🌐
Python Forum
python-forum.io › thread-33857.html
.json overwrites, rather than append??
Greetings, I am trying to write a Pandas DataFrame to a json file. The json file gets overwritten, rather than appended?? Can anyone spot the error?? Any pointers appreciated. The pprint statement returns 1 row with 4 columns .... hundreds of!! I o...
🌐
GitHub
github.com › bobbyhadz › how-to-update-json-file-in-python › blob › main › main.py
how-to-update-json-file-in-python/main.py at main · bobbyhadz/how-to-update-json-file-in-python
# How to update a JSON file in Python · · 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]) ·
Author: bobbyhadz
🌐
Python.org
discuss.python.org › python help
Appending JSON to same file - Python Help - Discussions on Python.org
January 5, 2023 - Hi, I need to make that within each request from api new dict will be appended to json. Now I have that each time data overwritten, but I need to append to existing file. How to achieve this? Code:(import requestsimpor…
🌐
AskPython
askpython.com › python-modules › python-read-json-file-modify
Python Read JSON File and Modify - AskPython
June 30, 2023 - Python has boolean values with the first letter in uppercase but JavaScript has it all lowercase. So due to these differences, we need to convert a dictionary into a JavaScript Object Notation compatible format. For this purpose, json.dumps are used.