auslander auslander - 5 months ago 13x
Python Question

List Indices in json in Python

I've got a json file that I've pulled from a web service and am trying to parse it. I see that this question has been asked a whole bunch, and I've read whatever I could find, but the json data in each example appears to be very simplistic in nature. Likewise, the json example data in the python docs is very simple and does not reflect what I'm trying to work with. Here is what the json looks like:

{"RecordResponse": {
"Id": blah
"Status": {
"state": "complete",
"datetime": "2016-01-01 01:00"
"Results": {
"resultNumber": "500",
"Summary": [
"Type": "blah",
"Size": "10000000000",
"OtherStuff": {
"valueOne": "first",
"valueTwo": "second"
"fieldIWant": "value i want is here"

The code block in question is:

jsonFile = r'C:\Temp\results.json'

with open(jsonFile, 'w') as dataFile:
json_obj = json.load(dataFile)
for i in json_obj["Summary"]:

Not only am I not getting into the field I want, but I'm also getting a key error on trying to suss out "Summary".

I don't know how the indices work within the array; once I even get into the "Summary" field, do I have to issue an index manually to return the value from the field I need?


The example you posted is not valid JSON (no commas after object fields), so it's hard to dig in much. If it's straight from the web service, something's messed up. If you did fix it with proper commas, the "Summary" key is within the "Results" object, so you'd need to change your loop to

with open(jsonFile, 'w') as dataFile:
    json_obj = json.load(dataFile)
    for i in json_obj["Results"]["Summary"]:

If you don't know the structure at all, you could look through the resulting object recursively:

def findfieldsiwant(obj, keyname="Summary", fieldname="fieldIWant"):
      for key,val in obj.items():
         if key == keyname:
            return [ d[fieldname] for d in val ]
            sub = findfieldsiwant(val)
            if sub:
               return sub
   except AttributeError: #obj is not a dict
   #keyname not found
   return None