简体   繁体   中英

How to update/change both keys and values separately (not dedicated key-value pair) in a deeply nested JSON in python 3.x

I have a JSON file where I need to replace the UUID and update it with another one. I'm having trouble replacing the deeply nested keys and values.

Below is my JSON file that I need to read in python, replace the keys and values and update the file.

JSON file - myfile.json

{
   "name": "Shipping box"
   "company":"Detla shipping"
   "description":"---"
   "details" : {
                "boxes":[
                        {
                        "box_name":"alpha",
                        "id":"a3954710-5075-4f52-8eb4-1137be51bf14"
                        },
                        {
                        "box_name":"beta",
                        "id":"31be3763-3d63-4e70-a9b6-d197b5cb6929"
                        }          ​
                ​     ]
                ​}

    "container": [
                    "a3954710-5075-4f52-8eb4-1137be51bf14":[],
                    "31be3763-3d63-4e70-a9b6-d197b5cb6929":[]     ​
                 ​]

     ​"data":[
               { 
                    "data_series":[],
                    "other":50
               },
               { 
                    "data_series":[],
                    "other":40
               },
               { 
                    "data_series":
                            {
                                "a3954710-5075-4f52-8eb4-1137be51bf14": 
                                    {
                                      {
                                        "dimentions":[2,10,12]
                                      }
                                    },
                                "31be3763-3d63-4e70-a9b6-d197b5cb6929": 
                                    {
                                      {
                                        "dimentions":[3,9,12]
                                      }
                                    }
                            },
                    "other":50
                }
            ]
}

I want achieve something like the following-

 "details": { "boxes":[ { "box_name":"alpha" "id":"replace_uuid" }, }. . . "data":[ { "data_series": { "replace_uuid": { { "dimentions":[2,10,12] } } ]

In such a type of deeply nested dictionary, how can we replace all the occurrence of keys and values with another string, here replace_uuid ?

I tried with pop() and dotty_dict but I wasn't able to replace the nested list.

I was able to achieve it in the following way-

def uuid_change():                 #generate a random uuid
    new_uuid = uuid.uuid4()
    return str(new_uuid)

dict = json.load(f)

for uid in dict[details][boxes]:
    old_id = uid['id']
    replace_id = uuid_change()
    uid['id'] = replace_id
    
    for i in range(n):
        for uid1 in dict['container'][i].keys()
           if uid1 == old_id:
               dict['container'][i][replace_id] 
                    = dict['container'][i].pop(uid1)     #replace the key
    
    for uid2 in dict['data'][2]['data_series'].keys()
        if uid2 == old_id:
           dict['data'][2]['data_series'][replace_id] 
                = dict['data'][2]['data_series'].pop(uid2)     #replace the key


    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM