Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update json file with python

Tags:

python

json

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": "" } 
like image 676
Igal Avatar asked Dec 19 '12 09:12

Igal


People also ask

How do I update JSON data in Python?

Updating a JSON object in Python is as simple as using the built-in update() function from the json package we have imported. The update method is used to add a new key-value pair to the JSON string that we declared in our code.

How do you append data to an existing JSON file in Python?

Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.

How do I edit a JSON file?

In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.


2 Answers

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() 
like image 63
Shawn Chin Avatar answered Sep 30 '22 18:09

Shawn Chin


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() 
like image 45
Igal Avatar answered Sep 30 '22 17:09

Igal