Json 密钥转储,配对 Python
Json dump for key, pair in Python
我有以下文件,它是 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"
我认为这个文件是乱七八糟的。它的格式不正确。我研究了如何在 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")
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"
},
]
现在我的问题
- json 转储文件在我看来并不像 json。我怎样才能让它成为正确的格式
- json 转储文件很大。所以我有键作为 ID,值是列表,里面有另一个字典。(我想是的)在这种情况下如何获取数据?
- 我需要检查 'resource_type' 是否为 OS::Cinder::Volume,我将如何获取它,否则如果我需要获取 resource_type 的值,我将如何获取它?
如果有人能向我解释我的 json 文件,那将会很有帮助。或者,如果没有,请将我指向可以帮助我理解嵌套词典的链接
已编辑:获取我在下面所做的值并给我
ValueError: too many values to unpack
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'])
data.json 如下
您的 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"),您需要访问列表中第一个元素的 'resource-status' 键,您只需使用以下内容:
myjson["fdd6a102-359c-4527-8469-4ef01a9c0076"][0]['resource-status']
更多信息可以found here.
对于最后一部分,您查看 this answer on nested JSON。
我有以下文件,它是 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"
我认为这个文件是乱七八糟的。它的格式不正确。我研究了如何在 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")
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"
},
]
现在我的问题
- json 转储文件在我看来并不像 json。我怎样才能让它成为正确的格式
- json 转储文件很大。所以我有键作为 ID,值是列表,里面有另一个字典。(我想是的)在这种情况下如何获取数据?
- 我需要检查 'resource_type' 是否为 OS::Cinder::Volume,我将如何获取它,否则如果我需要获取 resource_type 的值,我将如何获取它?
如果有人能向我解释我的 json 文件,那将会很有帮助。或者,如果没有,请将我指向可以帮助我理解嵌套词典的链接
已编辑:获取我在下面所做的值并给我
ValueError: too many values to unpack
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'])
data.json 如下
您的 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"),您需要访问列表中第一个元素的 'resource-status' 键,您只需使用以下内容:
myjson["fdd6a102-359c-4527-8469-4ef01a9c0076"][0]['resource-status']
更多信息可以found here.
对于最后一部分,您查看 this answer on nested JSON。