Update existing json file in python

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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within { }. 
     

    Functions Used:  

    • json.loads(): json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Syntax: json.loads(json_string)
    Parameter: It takes JSON string as the parameter.
    Return type: It returns the python dictionary object. 
     

    • json.dumps(): json.dumps() function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
       

    Syntax: json.dumps(object)
    Parameter: It takes Python Object as the parameter.
    Return type: It returns the JSON string. 
     

    • update(): This method updates the dictionary with elements from another dictionary object or from an iterable key/value pair.
       

    Syntax: dict.update([other])
    Parameters: Takes another dictionary or an iterable key/value pair.
    Return type: Returns None. 
     

    Example 1: Updating a JSON string.
      

    Python3

    import json

    x =  '{ "organization":"GeeksForGeeks",

            "city":"Noida",

            "country":"India"}'

    y = {"pin":110096}

    z = json.loads(x)

    z.update(y)

    print(json.dumps(z))

    Output:
     

    {“pin”: 110096, “organization”: “GeeksForGeeks”, “country”: “India”, “city”: “Noida”} 
     

    Example 2: Updating a JSON file. Suppose the JSON file looks like this.
     

    Update existing json file in python

    We want to add another JSON data after emp_details. Below is the implementation.

    Python3

    import json

    def write_json(new_data, filename='data.json'):

        with open(filename,'r+') as file:

            file_data = json.load(file)

            file_data["emp_details"].append(new_data)

            file.seek(0)

            json.dump(file_data, file, indent = 4)

    y = {"emp_name":"Nikhil",

         "email": "",

         "job_profile": "Full Time"

        }

    write_json(y)

    Output:
     

    Update existing json file in python


    How do I update an existing JSON file?

    Updating or Merging the Existing JSON File.
    Create a new. task..
    In. Definition. tab, select. Update. as the task operation..
    Follow the same procedure of creating a JSON Target file using Insert task operation to update the existing JSON file..

    How do I add data to an existing JSON object in Python?

    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 you clear a JSON file in Python?

    To delete a JSON object from a list: Parse the JSON object into a Python list of dictionaries. Use the enumerate() function to iterate over the iterate over the list. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.

    Which method will you use if you need to update a particular data JSON?

    We use JSON_MODIFY() function to update the JSON string. It can update the following items: Update existing property value. Add a new element in an existing array.