Hướng dẫn update value in json file python - cập nhật giá trị trong tệp json python

Tôi đang cố cập nhật tệp JSON hiện có, nhưng từ một số lý do, giá trị được yêu cầu không được thay đổi nhưng toàn bộ tập hợp các giá trị (với giá trị mới) đang được thêm vào tệp gốc

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


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

jsonFile.write(json.dumps(data))

và kết quả là: Yêu cầu:

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

Cập nhật nội dung tệp JSON của Python với các ví dụ mã

Xin chào mọi người, trong bài đăng này, chúng tôi sẽ kiểm tra cách giải câu đố lập trình nội dung tệp JSON cập nhật Python.

import json

def repJsonObj(file, object, value):
    f = open(f"{file}", "r")
    json_object = json.load(f)
    f.close()

    json_object[object] = value

    f = open(file, "w")
    json.dump(json_object, f)
    f.close()


repJsonObj('test.json', "num", 2713490127)

Chúng tôi đã trình bày rất nhiều ví dụ minh họa để cho thấy cách giải quyết vấn đề nội dung tệp JSON của Python Cập nhật và chúng tôi cũng đã giải thích cách làm như vậy.

'{ 0'{ 6'{ 22

Bạn có thể chỉnh sửa tệp JSON bằng Python không?

  • Có một số cách để cập nhật tệp JSON trong Python. Ở đây, tôi sẽ cung cấp cho bạn ví dụ rất đơn giản về tệp JSON chỉnh sửa bằng các hàm Open (), append (), dump () và đóng (). Vì vậy, hãy xem một ví dụ đơn giản dưới đây: bạn có thể sử dụng các ví dụ này với phiên bản Python3 (Python 3).
  • Chúng tôi có thể cập nhật tệp JSON không?
  • '{ 0'{ 6'{ 22

    Bạn có thể chỉnh sửa tệp JSON bằng Python không?

    Có một số cách để cập nhật tệp JSON trong Python. Ở đây, tôi sẽ cung cấp cho bạn ví dụ rất đơn giản về tệp JSON chỉnh sửa bằng các hàm Open (), append (), dump () và đóng (). Vì vậy, hãy xem một ví dụ đơn giản dưới đây: bạn có thể sử dụng các ví dụ này với phiên bản Python3 (Python 3).
     

    Các chức năng được sử dụng: & nbsp; & nbsp;  

    • json.loads (): hàm json.loads () có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để phân tích chuỗi JSON. & NBSP;json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Cú pháp: tham số JSON.LOADS (JSON_STRING): Nó lấy chuỗi JSON làm tham số.return loại: nó trả về đối tượng từ điển Python. & Nbsp; & nbsp; json.loads(json_string)
    Parameter: It takes JSON string as the parameter.
    Return type: It returns the python dictionary object. 
     

    • json.dumps (): hàm json.dumps () có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để chuyển đổi đối tượng Python thành Chuỗi JSON. & NBSP;json.dumps() function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
       

    Cú pháp: json.dumps (Đối tượng) Tham số: Nó lấy đối tượng Python làm tham số.return loại: nó trả về chuỗi JSON. & Nbsp; & nbsp; json.dumps(object)
    Parameter: It takes Python Object as the parameter.
    Return type: It returns the JSON string. 
     

    • CẬP NHẬT (): Phương thức này cập nhật từ điển với các phần tử từ một đối tượng từ điển khác hoặc từ một cặp khóa/giá trị có thể lặp lại. & NBSP; This method updates the dictionary with elements from another dictionary object or from an iterable key/value pair.
       

    Cú pháp: Dict.Update ([Khác]) Tham số: Lấy một từ điển khác hoặc một cặp khóa/giá trị có thể lặp lại. dict.update([other])
    Parameters: Takes another dictionary or an iterable key/value pair.
    Return type: Returns None. 
     

    Ví dụ 1: Cập nhật chuỗi JSON. & NBSP; & NBSP; Updating a JSON string.
      

    Python3

    import json

    ________ 6 ________ 7 & nbsp; '{ "organization"____1011

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

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    3
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    9
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    0
    {
    "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": ""
    }
    
    1
    {
    "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": ""
    }
    
    2

    {
    "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": ""
    }
    
    3=
    {
    "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": ""
    }
    
    5
    {
    "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": ""
    }
    
    6
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    0
    {
    "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": ""
    }
    
    8
    {
    "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": ""
    }
    
    9

    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    0____7
    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    2

    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    3

    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    4
    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    5

    Đầu ra: & nbsp;
     

    {Pin Pin Cảnh: 110096, Tổ chức của người Hồi giáo
     

    Ví dụ 2: Cập nhật tệp JSON. Giả sử tệp JSON trông như thế này. & NBSP; Updating a JSON file. Suppose the JSON file looks like this.
     

    Hướng dẫn update value in json file python - cập nhật giá trị trong tệp json python

    Chúng tôi muốn thêm một dữ liệu JSON khác sau EMP_DETAILS. Dưới đây là việc thực hiện.

    Python3

    import json

    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    8
    import json
    
    def repJsonObj(file, object, value):
        f = open(f"{file}", "r")
        json_object = json.load(f)
        f.close()
    
        json_object[object] = value
    
        f = open(file, "w")
        json.dump(json_object, f)
        f.close()
    
    
    repJsonObj('test.json', "num", 2713490127)
    9=import1import2

    import3import4import5import6import7import8import9

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

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

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

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    3import9x 3x 4json6

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    3x 7import9x 9= =1json6

    Các

    '{ 0'{ 1'{ 2'{ 3

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

    '{ 0'{ 6'{ 22

    import3

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

    "organization"1

    Output: 
     

    Hướng dẫn update value in json file python - cập nhật giá trị trong tệp json python


    Bạn có thể chỉnh sửa tệp JSON bằng Python không?

    Có một số cách để cập nhật tệp JSON trong Python.Ở đây, tôi sẽ cung cấp cho bạn ví dụ rất đơn giản về tệp JSON chỉnh sửa bằng các hàm Open (), append (), dump () và đóng ().Vì vậy, hãy xem một ví dụ đơn giản dưới đây: bạn có thể sử dụng các ví dụ này với phiên bản Python3 (Python 3).edit JSON file using open(), append(), dump() and close() functions. so let's see a simple example below: You can use these examples with python3 (Python 3) version.

    Chúng tôi có thể cập nhật tệp JSON không?

    Bạn có thể sử dụng chức năng Oracle SQL JSON_MERGEPATCH để cập nhật các phần cụ thể của tài liệu JSON.use Oracle SQL function json_mergepatch to update specific portions of a JSON document.

    JSON Dump Python là gì?

    Phương thức Dump () được sử dụng khi các đối tượng Python phải được lưu trữ trong một tệp.Các bãi () được sử dụng khi các đối tượng được yêu cầu ở định dạng chuỗi và được sử dụng để phân tích cú pháp, in, v.v.Dump () cần tên tệp JSON trong đó đầu ra phải được lưu trữ dưới dạng đối số.used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.