I'm trying to simply grab information from a json file using python. I've seen many threads that briefly cover this, however there are some questions that I have that I have not seen answered on here.
Let's say I have the following json file:
{
"href" : "http://www.google.com",
"Hosts" : {
"cluster_name" : "MyCluster",
"cpu_count" : 4,
"disk_info" : [
{
"available" : "78473288",
"used" : "15260444",
},
{
"available" : "4026904",
"used" : "8",
},
{
"available" : "110454",
"used" : "73547",
}
]
}
}
I understand that I can have the following code:
import json
#Store JSON data into json_file
json_data = json.loads(json_file)
print json_data["Hosts"]
And the output of this code would give me everything that is in hosts:
"cluster_name" : "MyCluster",
"cpu_count" : 4,
"disk_info" : [
{
"available" : "78473288",
"used" : "15260444",
},
{
"available" : "4026904",
"used" : "8",
},
{
"available" : "110454",
"used" : "73547",
}
]
However, how do I grab specific values from specific lines within this json file that has multiple lines of data embedded in others?
For example, what if I wanted to get the value of "cpu_count" from "Hosts"? Or for an even more difficult example, how would I get the second listing of "available" that is inside "disk_info" which is inside "Hosts"?
I'm much more interested in finding a way to grab specific values within a json file, not an entire list of data.
Any help is appreciated, and please point it out if there is already a thread that ACTUALLY covers this. Thank you!