Question

I'm having trouble getting the proper output value when doing

print metadata["File:FileSize"]

It gives me list indices must be a integer, not str error. I thought json.loads returns a dictionary.

The variable metadata is retrieved like this from exiftool in a class as such:

def get_metadata(self, *filenames):
    return json.loads(self.execute("-G", "-j", "-n", *filenames))

and in the main program flow I do this:

metadata = exif.get_metadata(fileName)
print metadata
print metadata["File:FileSize"]

Can someone help me figure out what am I doing wrong?

Here is a sample of a raw exiftool query:

[{
  "SourceFile": "/media/mango/MF-HDD-277/01_audio/Computer Arts royalty-free audio samples/Disc 191 - soundsnap/ComputerArtsDisc191_SoundSnapSamples_088_Bend03.wav",
  "ExifTool:ExifToolVersion": 9.13,
  "File:FileName": "ComputerArtsDisc191_SoundSnapSamples_088_Bend03.wav",
  "File:Directory": "/media/mango/MF-HDD-277/01_audio/Computer Arts royalty-free audio samples/Disc 191 - soundsnap",
  "File:FileSize": 146948,
  "File:FileModifyDate": "2010:10:24 11:17:20-04:00",
  "File:FileAccessDate": "2014:01:29 15:58:48-05:00",
  "File:FileInodeChangeDate": "2014:01:21 13:28:00-05:00",
  "File:FilePermissions": 711,
  "File:FileType": "WAV",
  "File:MIMEType": "audio/x-wav",
  "RIFF:Encoding": 1,
  "RIFF:NumChannels": 2,
  "RIFF:SampleRate": 44100,
  "RIFF:AvgBytesPerSec": 176400,
  "RIFF:BitsPerSample": 16,
  "Composite:Duration": 0.833038548752834
}]
Was it helpful?

Solution

The raw query is returning a javascript object inside an array. When you parse the JSON with json.loads, you get the dict inside a list. So, to access the attributes, do metadata[0]["File:FileSize"].

Or change the get_metadata method to directly return the dict.

def get_metadata(self, *filenames):
    return json.loads(self.execute("-G", "-j", "-n", *filenames))[0]

And no, json.loads not only returns dictionaries. The table Tim linked in his comment specifies the JSON to Python translations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top