119

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": ""
}
0

4 Answers 4

205

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()
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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?
Does json.dump(data, jsonFile); jsonFile.truncate() updates only the update section on the file or writes complete file all over again? @Shawn Chin
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.
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.
|
55
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()

2 Comments

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 ?
i guess it is just an example to show possibilities how "Working with buffered content" works, as the comment states.
-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()

1 Comment

Please, add a brief explanation of how/why it solves the problem.
-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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.