Search code examples
pythonjsondata-extraction

How can I extract one value from this JSON ARRAY?


This is the data I get from MPI ELAN, it's a about movie file used in that program:

'[{"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1.avi", "MIME_TYPE": "video/*", "TIME_ORIGIN": "176040"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1-LEWA.mp4", "MIME_TYPE": "video/mp4", "TIME_ORIGIN": "100408"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1-PRAWA.mp4", "MIME_TYPE": "video/mp4", "TIME_ORIGIN": "82199"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1.mp3", "MIME_TYPE": "audio/*"}]'

I'd like to extract the value from this key: "TIME_ORIGIN": "82199" and I think this is JSON ARRAY, but I can't do it.


Solution

  • You need to use json.loads(load json from string):

    Here is a working example:

    import json
    
    my_json_string = '[{"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1.avi", "MIME_TYPE": "video/*", "TIME_ORIGIN": "176040"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1-LEWA.mp4", "MIME_TYPE": "video/mp4", "TIME_ORIGIN": "100408"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1-PRAWA.mp4", "MIME_TYPE": "video/mp4", "TIME_ORIGIN": "82199"}, {"MEDIA_URL": "file:///Volumes/MINI RUGGED/MULTIMET2015/JOINT/29-60-S1.mp3", "MIME_TYPE": "audio/*"}]'
    
    my_json = json.loads(my_json_string)
    
    print(my_json[0]['TIME_ORIGIN'])