[英]Json dump for key, pair in Python
I have below file which is the result of a json dump.我有以下文件,它是 json 转储的结果。
"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"
This file is messed up I think.我认为这个文件搞砸了。 It is not in the right format.
它的格式不正确。 I researched on how to dump in json
我研究了如何在 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")
The output of openstack stack resource list -f json comes in below format openstack stack resource list -f json 的输出格式如下
[
{
"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"
},
]
Now my problems现在我的问题
It will be helpful if someone can explain me my json file.如果有人可以向我解释我的 json 文件会很有帮助。 Or if not, please direct me to the links that could help me understand nested dictionaries
或者,如果没有,请将我指向可以帮助我理解嵌套字典的链接
Edited : To fetch the value I did below and is giving me编辑:为了获取我在下面所做的并给我的值
ValueError: too many values to unpack
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'])
Your JSON dump can be viewed as a simple dictionary in python.您的 JSON 转储可以被视为 Python 中的一个简单字典。 So for your first part you can use the following :
因此,对于您的第一部分,您可以使用以下内容:
import json
#Assuming 'mydict' contains the json dump
with open('out.json', 'w') as outfile:
json.dump(mydict, outfile)
Now coming to the second part, suppose for the first element (fdd6a102-359c-4527-8469-4ef01a9c0076") in your example you need to access the 'resource-status' key of the first element in the list, you simply need to use the following:现在进入第二部分,假设对于示例中的第一个元素 (fdd6a102-359c-4527-8469-4ef01a9c0076"),您需要访问列表中第一个元素的 'resource-status' 键,您只需要使用以下内容:
myjson["fdd6a102-359c-4527-8469-4ef01a9c0076"][0]['resource-status']
More information can be found here .可以在此处找到更多信息。
For your final part, you view this answer on nested JSON .对于最后一部分,您在嵌套的 JSON 上查看此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.