Extract info from json file, Python

161 Views Asked by At

I am trying to extract information out of a JSON file that I dumped. I used Mido module to get the information I need, and the only way I've found to get these features is by dumping it as a JSON. But, after some search, I've tried and not managed to extract and store the info in a python array (numpy array)* Below, you see the sample code. I am pretty sure that double [[ at the beginning of the file is making all this trouble.

if __name__ == '__main__':
filename = 'G:\\ΣΧΟΛΗ p12127\\Πτυχιακη\\MAPS Dataset\\MAPS_AkPnBcht_1\\AkPnBcht\\ISOL\\CH\\MAPS_ISOL_CH0.1_F_AkPnBcht.mid'

midi_file = mdfile(filename)

for i, track in enumerate(midi_file.tracks):
    print('Track: ', i,'\nNumber of messages: ',  track)
    for message in track:
        print(message)
def midifile_to_dict(mid):
    tracks = []
    for track in mid.tracks:
        tracks.append([vars(msg).copy() for msg in track])

return {
    'ticks_per_beat': mid.ticks_per_beat,
    'tracks': tracks,
}


mid = mido.MidiFile('G:\\ΣΧΟΛΗ p12127\\Πτυχιακη\\MAPSDataset\\MAPS_AkPnBcht_1\\AkPnBcht\\ISOL\\CH\\MAPS_ISOL_CH0.1_F_AkPnBcht.mid')

dataj = json.dumps(midifile_to_dict(mid), indent=2)
data = json.loads(dataj)

Output :

  {
  "ticks_per_beat": 32767,
  "tracks": [[
  {
  "type": "set_tempo",
  "tempo": 439440,
  "time": 0
 },
 {
"type": "end_of_track",
    "time": 0
}
  ]]
}

So, to close this up, how could I extract this info? Is JSON even a good approach? And if so, how could I actually extract this info? Or a way to go inside the [[ ]].
[Windows 7, Python 3.6+]

0

There are 0 best solutions below