I need to parse the below Json data using python and write to a csv file. Below I have included only 2 server names but my list is big. Please help with sample code to get the desired output.
Below is my json data in a file server_info.json:
{
"dev-server":
{
"hoststatus":
{
"host_name":"dev-server",
"current_state":"2",
"last_time_up":"1482525184"
},
"servicestatus":
{
"/ Filesystem Check":
{
"host_name":"dev-server",
"service_description":"/ Filesystem Check",
"current_state":"1",
"state_type":"1"
},
"/home Filesystem Check":
{
"host_name":"dev-server",
"service_description":"/home Filesystem Check",
"current_state":"2",
"state_type":"2"
}
}
},
"uat-server":
{
"hoststatus":
{
"host_name":"uat-server",
"current_state":"0",
"last_time_up":"1460000000"
},
"servicestatus":
{
"/ Filesystem Check":
{
"host_name":"uat-server",
"service_description":"/ Filesystem Check",
"current_state":"0",
"state_type":"1"
},
"/home Filesystem Check":
{
"host_name":"uat-server",
"service_description":"/home Filesystem Check",
"current_state":"1",
"state_type":"2"
}
}
}
}
Expected Output:
output format:
hoststatus.host_name,hoststatus.current_state,hoststatus.last_time_up
-------------------------------------------------------------
dev-server,2,1482525184
uat-server,0,1460000000
and
output format:
servicestatus.host_name,servicestatus.service_description,servicestatus.current_state,servicestatus.state_type
--------------------------------------------------------------------------------
dev-server,/ Filesystem Check,1,1
dev-server,/home Filesystem Check,2,2
uat-server,/ Filesystem Check,0,1
uat-server,/home Filesystem Check,1,2
json.loads()to get a python dict, then show us your attempts. this is a tedious code-writing task, better done by you.