How to update json file with python

214,883

Solution 1

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()

Solution 2

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()
Share:
214,883
Igal
Author by

Igal

Updated on August 07, 2020

Comments

  • Igal
    Igal over 3 years

    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": ""
    }
    
  • BoltzmannBrain
    BoltzmannBrain about 8 years
    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.
  • SMDC
    SMDC about 5 years
    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 ?
  • alper
    alper about 5 years
    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
    alper about 5 years
    Does json.dump(data, jsonFile); jsonFile.truncate() updates only the update section on the file or writes complete file all over again? @Shawn Chin
  • Christoph Lösch
    Christoph Lösch almost 5 years
    i guess it is just an example to show possibilities how "Working with buffered content" works, as the comment states.
  • Sun Bear
    Sun Bear about 4 years
    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.
  • 42piratas
    42piratas about 3 years
    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.
  • BourbonCreams
    BourbonCreams almost 2 years
    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.