'Getting values from json nested list (VK api on Python)

I am making requests to VK using api in python. I need to collect all the images from the group wall. I tried to do it with a parser, but I get request limits. So I started using this approach. I need to get links to images. Due to the fact that the social network makes several copies, in response to the request, I get the following json structure.

Link on docs vk api (photo.get): https://dev.vk.com/method/photos.get

[
{"album_id": -7,
 "date": 1647940135,
 "id": 457246299,
 "owner_id": -193322879,
 "post_id": 16562,
 "sizes": [
 {"height": 130, "url": "https://sun9-64.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=130x130&quality=96&sign=161ce4dbcc300d5434f5332306a54f14&c_uniq_tag=EJOoWWBqMneHuY0UOJm1M9RnRzZJcOI-zJfzHKDNda4&type=album", "type": "m", "width": 130},
 {"height": 130, "url": "https://sun7-6.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=130x130&quality=96&sign=161ce4dbcc300d5434f5332306a54f14&c_uniq_tag=EJOoWWBqMneHuY0UOJm1M9RnRzZJcOI-zJfzHKDNda4&type=album", "type": "o", "width": 130}, 
 {"height": 200, "url": "https://sun7-6.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=200x200&quality=96&sign=d0f4df2eeec7b1395292fd10f0a0e47f&c_uniq_tag=14xJ5JVQks6Vy9belwTdpZAqODJa3l76EAQuXaSnSes&type=album", "type": "p", "width": 200}, 
 {"height": 320, "url": "https://sun7-6.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=320x320&quality=96&sign=03c03d728520a413f611cdf0db6083b4&c_uniq_tag=vezxYPaxbmSJM7GwdklhYhM6ZBqquVudd3T2Y7XS5PY&type=album", "type": "q", "width": 320}, 
 {"height": 510, "url": "https://sun7-6.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=510x510&quality=96&sign=dd9cdd42f232b9f96409bad84ace87cd&c_uniq_tag=N3l2geecxWc_SipOMG2as9Pn4dQZZGdQ96F5NJRsBNo&type=album", "type": "r", "width": 510}, 
 {"height": 75, "url": "https://sun9-64.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=75x75&quality=96&sign=02846e1702ab3e0a7cd70122acaa0a6c&c_uniq_tag=JregusGVj2BPy6ojKAe51n1RJQIC-tenm5w4mLtfWlU&type=album", "type": "s", "width": 75}, 
 {"height": 604, "url": "https://sun9-64.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=604x604&quality=96&sign=9fec9e9d5f51c5640baaf21257bf679c&c_uniq_tag=4BS5xJA73UTeGXw58Fc6WS21AVLDBE2oH3NXjVa7thU&type=album", "type": "x", "width": 604}, 
 {"height": 807, "url": "https://sun9-64.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=807x807&quality=96&sign=be189a96a131049788a79dc1dd4d25ca&c_uniq_tag=9aXBOQlWEh7S96DwhVYM-NleGXzcd5iTBEH2ZjGxfCI&type=album", "type": "y", "width": 807}, 
 {"height": 1000, "url": "https://sun7-6.userapi.com/impf/mxRWsDaK1gMa-g6EKD0QEpl3NjWszqKTlbF5_Q/Bn71Q2tYx58.jpg?size=1000x1000&quality=96&sign=a7b44523aae37593fbf1cf3d6e08a86f&c_uniq_tag=rHMEBYwXrIG5_GehlbEFMMHKRXa5PG5FyzyOVCUN2oQ&type=album", "type": "z", "width": 1000}
],
"text": "",
 "user_id": 100,
 "has_tags": false},


{"album_id": -7,
 "date": 1647940118,
 "id": 457246298,
 "owner_id": -193322879,
 "sizes": [
{"height": 130, "url": "https://sun9-25.userapi.com/impf/fO6mezXjIJVIWA0IWPe.....

Further, I want to collect all this into a dataframe, so that later it would be just more convenient to directly download images from links. But I'm having difficulty identifying the entries. The "size" field contains links to images of different sizes, and I only need images with the highest resolution. How do I access the fields of interest that are in the "size" list?

To form the structure, I use the following approach:

def make_image_dataframe(all_photos):
    filtered_data = []
    for image in all_photos:

        try:
            id = image['id']
        except:
            id = 0

        try:
            post_id = image['post_id']
        except:
            post_id = 0

The structure I want to have in the aftermath (dataframe):

id   post_id   date   time   timestamp  link_x_type   link_y_type   link_z_type


Solution 1:[1]

I figured out the following worked for me:

def find_by_key(iterable, key, value):
    for index, dict_ in enumerate(iterable):
        if key in dict_ and dict_[key] == value:
            return (dict_)

and in the formation of the structure

try:
   find_x_type = find_by_key(image['sizes'], 'type', 'x')
   x_type = find_x_type['url']
except:
   x_type = ''

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kostya ivanov