112

I'm trying to update existing Json file, but from some reason, the requested value is not being changed but the entire set of values (with the new value) is being appended to the original file

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


tmp = data["location"]
data["location"] = "NewPath"

jsonFile.write(json.dumps(data))

and the result is : Required:

{
   "location": "NewPath",
   "Id": "0",
   "resultDir": "",
   "resultFile": "",
   "mode": "replay",
   "className":  "",
   "method":  "METHOD"
}

Actual:

{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className":  "",
"method":  "METHOD"
}
{
    "resultDir": "",
    "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
    "method": "METHOD",
    "className": "",
    "mode": "replay",
    "Id": "0",
    "resultFile": ""
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
Igal
  • 4,603
  • 14
  • 41
  • 66

4 Answers4

198

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()
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • 19
    Thank you for explaining the use of `seek()` and `truncate()`. I would however improve this answer by changing `jsonFile.write(json.dumps(data))` to `jsonFile.dump(data, f)`; more pythonic. – BoltzmannBrain Oct 16 '15 at 18:35
  • 2
    If I am updating multiple locations such as `data["location_2"] = "NewPath_2"` as well, should I do `jsonFile.seek(0); dump(); truncate()` for that line also or only doing single `jsonFile.seek(0) ; json.dump(data, jsonFile); jsonFile.truncate()` on the end for the all updates would be enough? – alper Oct 26 '18 at 13:11
  • 2
    Does `json.dump(data, jsonFile); jsonFile.truncate()` updates only the update section on the file or writes complete file all over again? @Shawn Chin – alper Oct 26 '18 at 13:21
  • In the second example, `tmp = data["location"] ` appears to be redundant; it should be removed. Also, I used` json.dump(data, jsonFile, indent=4)` to trigger pretty-print, in this way the json file layout will not be the compact type. – Sun Bear Oct 08 '19 at 14:44
  • Isn't there a way to update a single json field only, without having to rewrite the entire file? I was just playing with it and end up messing an entire file. – 42piratas Oct 13 '20 at 02:13
  • I had issues with the json.load() function. I figured out after some time that it was failing because of some comments in the JSON file. Once I removed them, it worked perfectly. Hope it helps someone. – BourbonCreams Feb 01 '22 at 12:11
  • very well explained ! – Francois Sep 09 '22 at 13:26
54
def updateJsonFile():
    jsonFile = open("replayScript.json", "r") # Open the JSON file for reading
    data = json.load(jsonFile) # Read the JSON into the buffer
    jsonFile.close() # Close the JSON file

    ## Working with buffered content
    tmp = data["location"] 
    data["location"] = path
    data["mode"] = "replay"

    ## Save our changes to JSON file
    jsonFile = open("replayScript.json", "w+")
    jsonFile.write(json.dumps(data))
    jsonFile.close()
Community
  • 1
  • 1
Igal
  • 4,603
  • 14
  • 41
  • 66
  • 9
    I'm curious, what's the need of tmp variable? is there a difference of passing it to tmp = data['location] versus directly data['location'] path ? – SMDC Aug 24 '18 at 03:51
  • 3
    i guess it is just an example to show possibilities how "Working with buffered content" works, as the comment states. – Christoph Lösch Jan 22 '19 at 19:26
-1
def updateJsonFile():   
    with open(os.path.join(src, "replayScript.json"), "r+") as jsonFile:
        data = json.load(jsonFile)
        jsonFile.truncate(0)
        jsonFile.seek(0)
        data["src"] = "NewPath"
        json.dump(data, jsonFile, indent=4)
        jsonFile.close()
RCM696
  • 1
-1
def writeConfig(key, value):
        with open('config.json') as f:
            data = json.load(f)
            # Check if key is in file
            if key in data:
                # Delete Key
                del data[key]
                cacheDict = dict(data)
                # Update Cached Dict
                cacheDict.update({key:value})
                with open(dir_path + 'config.json', 'w') as f:
                    # Dump cached dict to json file
                    json.dump(cacheDict, f, indent=4)