I have below file which is the result of a json dump.
"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
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
[
{
"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
- The json dump file doesn't really look like json to me. How can I get it to be in proper format
- The json dump file is a big one. so I have key as the ID and the value is the list inside which there is another dictionary.(I think so) How do I fetch data in such scenario?
- I need to check for example if 'resource_type' is OS::Cinder::Volume, how will I get it or else if I need to get the value of resource_type, how will I get it?
It will be helpful if someone can explain me my json file. 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
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'])