繁体   English   中英

如何在 python 3.x 中深度嵌套的 JSON 中分别更新/更改键和值(不是专用键值对)

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

我有一个 JSON 文件,我需要在其中替换 UUID 并用另一个更新它。 我无法替换深度嵌套的键和值。

下面是我需要在 python 中读取的 JSON 文件,替换键和值并更新文件。

JSON 文件 - 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
                }
            ]
}

我想实现以下目标 -

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

在这种深度嵌套的字典中,我们如何用另一个字符串替换所有出现的键和值,这里是replace_uuid

我尝试使用pop()dotty_dict但我无法替换嵌套列表。

我能够通过以下方式实现它 -

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


    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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