2017-08-02 29 views
0

我有下面的文件,這是JSON轉儲的結果。Json dump for key,在Python中對

"fdd6a102-359c-4527-8469-4ef01a9c0076": "[\n {\n \"resource_status\": \"CREATE_COMPLETE\", \n \"resource_name\": \"i4_instance_internal_port\", \n \"resource_type\": \"OS::Neutron::Port\", \n \"physical_resource_id\": \"5db1d412-9a43-45c7-b72d-0dbe4eb16497\", \n \"updated_time\": \"2017-07-14T09:00:44\"\n }, \n {\n \"resource_status\": \"CREATE_COMPLETE\", \n \"resource_name\": \"i3_instance\", \n \"resource_type\": \"OS::Nova::Server\", \n \"physical_resource_id\": \"50375d90-5b57-412e-afe3-fdddefbd2f41\", \n \"updated_time\": \"2017-07-14T09:00:44\"\n }, \n {\n \"resource_status\": \"CREATE_COMPLETE\", \n \"resource_name\": \"i3_v1_instance_volume\", \n \"resource_type\": \"OS::Cinder::Volume\", \n \"physical_resource_id\": \"6750dc3d-e682-4a0c-a177-83a7252822fb\", \n \"updated_time\": \"2017-07-14T09:00:44\"\n }\n]\n" 

我覺得這個文件搞砸了。它的格式不正確。我研究瞭如何在JSON傾倒

def pp_another_json(myDict): 
    import io 
    try: 
     to_unicode = unicode 
    except NameError: 
     to_unicode = str 

    # Write JSON file 
    with io.open('data.json', 'w', encoding='utf8') as outfile: 
     str_ = json.dumps(myDict, 
          indent=4, sort_keys=True, 
          ensure_ascii=False) 
     outfile.write(to_unicode(str_)) 


class getstackList(): 
    def getStackID(self): 
     stacks = get_objects('stacks') 
     myDict = {} 
     for stack in stacks: 
      try: 
       myDict[stack.id] = subprocess.check_output(["openstack", "stack", "resource", "list", stack.id, "-f", "json"]) 
       pp_another_json(myDict) 
      except subprocess.CalledProcessError as e: 
       print("Error") 

-f JSON的OpenStack堆棧資源列表的輸出來在下面的格式

[ 
    { 
    "resource_status": "CREATE_COMPLETE", 
    "resource_name": "i4_instance_internal_port", 
    "resource_type": "OS::Neutron::Port", 
    "physical_resource_id": "5db1d412-9a43-45c7-b72d-0dbe4eb16497", 
    "updated_time": "2017-07-14T09:00:44" 
    }, 
    { 
    "resource_status": "CREATE_COMPLETE", 
    "resource_name": "i3_instance", 
    "resource_type": "OS::Nova::Server", 
    "physical_resource_id": "50375d90-5b57-412e-afe3-fdddefbd2f41", 
    "updated_time": "2017-07-14T09:00:44" 
    }, 
] 

現在我的問題

  1. JSON的轉儲文件沒有按我真的不像json。我怎樣才能得到它在適當的格式
  2. JSON轉儲文件是一個大的。所以我有關鍵的ID和值是其中有另一個字典的列表(我這樣認爲)如何在這種情況下獲取數據?
  3. 我需要檢查例如'resource_type'是OS :: Cinder :: Volume,我將如何得到它,否則,如果我需要獲取resource_type的值,我將如何得到它?

如果有人能解釋我我的json文件,這將會有所幫助。如果還是不行,請告訴我去,可以幫助我瞭解的嵌套字典

編輯鏈接:要取我下面做的價值,是給我ValueError異常:值過多解壓

with open('data.json') as data_file: 
     data_loaded = json.load(data_file) 
     for key, value in data_loaded: 
      print(data_loaded[key][0]['resource_status']) 

數據。 json低於 enter image description here

+0

你可以給data_loaded.values()的輸出嗎? –

+0

鍵值相同,data_loaded中的值相同: ValueError:解壓值太多 –

+0

您可以打印data_loaded並顯示輸出快照嗎? –

回答

0

您的JSON轉儲可以被視爲一個簡單的Python字典。因此,對於你的第一個部分,你可以使用以下命令:

import json 
#Assuming 'mydict' contains the json dump 

with open('out.json', 'w') as outfile: 
    json.dump(mydict, outfile) 

現在來到第二部分,假設在您的例子中,第一個元素(fdd6a102-359c-4527-8469-4ef01a9c0076" ),你需要訪問「資源狀態」列表中的第一個元素的鍵,您只需使用以下命令:。

myjson["fdd6a102-359c-4527-8469-4ef01a9c0076"][0]['resource-status'] 

更多信息可以found here

爲了您的最後一部分,您可以查看this answer on nested JSON

+0

請檢查我的編輯 –