1

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!

pnuts
  • 58,317
  • 11
  • 87
  • 139
OMGitzMidgar
  • 689
  • 2
  • 10
  • 28

2 Answers2

2

If you understand that you can do json_data["Hosts"], what's stopping you taking the same principle further?

json_data["Hosts"]["cpu_count"]
json_data["Hosts"]["disk_info"][1]["available"]

and so on.

Note that this has nothing at all to do with JSON: this is a perfectly normal Python data structure consisting of dicts and lists.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • See, from researching and trying things on my own, what you just said makes perfect sense. However, when I try to get the value from "available" by using the following line of code: " print json_data["Hosts"]["disk_info"][0]["available"] " I get the following error: KeyError: 'available' – OMGitzMidgar Jun 17 '15 at 17:35
  • @OMGitzMidgar So then its not that disk_info isnt working, its available. you should check that `json_data["Hosts"]["disk_info"][0]` is what you expect – rady Jun 17 '15 at 17:38
  • @Daniel How we can handle null cases here efficiently? How can we avoid exception in case if "cpu_count" is not present in JSON. How can we make sure to set a default value – Sebastian Apr 18 '18 at 09:21
2

Json.loads will also decode that dictionary. So to access cpu_count for example it would be json_data["Hosts"]["cpu_count"].

The json library will turn everything into a standard python data type (dict, list, int, str) or from those data type into a json readable format. You can also define a parser to parse from a class into json.

rady
  • 428
  • 5
  • 12