-1

i'm trying to access file_id but its in a list.

this is a part of the code update.message.photo.file_id

i am getting this error: 'list' object has no attribute 'file_id'

here's the json file:

{
  'update_id': 703304245,
  'message': {
    'photo': [
      {
        'width': 90,
        'height': 51,
        'file_id': 'AgACAgQAAx0CU5MMFAACDgFj_wV1c6WOBWXU_GYGLwT9BV9cngACw7sxG93l-VMAAfavUwejsMMBAAMCAANzAAMuBA',
        'file_size': 1293,
        'file_unique_id': 'AQADw7sxG93l-VN4'
      },
    ],
Shadmehr
  • 3
  • 3
  • It's a dict with key `photo`, which has value that is list of dicts, and these dict(s) have key `file_id` – buran Mar 01 '23 at 08:10
  • Your error message is strange. It looks like you could actually get the `photo` list. But that shouldn't be possible by attribute access. – Klaus D. Mar 01 '23 at 08:58

2 Answers2

0

You can access it this way:

file['message']['photo'][0]['file_id']

I presumed this dictionary is called file

Edo
  • 16
  • 3
0

your_dictionary['message']['photo'][0]['file_id']

And replace 'your_dictionary' with the name of the file, I think, or call it something when you're accessing it?

Essentially, there is a list inside the nested dictionary, and to get the dictionary inside it out you need to get the first element [0] from that list

Alternatively a json parsing function could help?

b_d
  • 1
  • 1
  • 1