Can't extend JSON object

api_boxoffice = requests.get(f'{url}/8495191/box-office', headers=headers, data=payload)
boxoffice = api_boxoffice.json()["boxOffice"]

for row in event_ids:
    bo_url = f"{url}/{row}/box-office"
    api_boxoffice = requests.get(bo_url, headers=headers, data=payload)
    boxoffice.extend(api_boxoffice.json()["boxOffice"])

When I attempt to execute the last line I get AttributeError: ‘dict’ object has no attribute ‘extend’
I am getting a valid response back from the API call, and I do a similar extend inside a for loop earlier in my code that works. Any help in resolving this is much appreciated.

Yes, exactly as it says. JSON represents a tree structure containing either lists or dictionaries, in any combination. api_boxoffice.json()["boxOffice"] means to read and parse the JSON from the request, then look up whatever’s under the boxOffice key. (This worked, so the top level of the JSON must have been a dict). The thing that you get back can be any type that JSON represents. If it’s not a list, then you won’t be able to .extend it. The error message clearly tells you that it was, instead, another dict.

Yes; that’s because you had a list that time.

We can’t possibly tell you any more than this without actually seeing the structure of the JSON. But chances are that wouldn’t be helpful anyway. You will learn more (assuming that there’s actually something you didn’t understand here - sometimes people just overlook something simple, or don’t think straight, after all) by studying the data yourself, and thinking about what you actually want to do with it.

2 Likes

Thank you Karl,

Turns out actually reading the data returned helped solve the issue :blush: (it only returns dict not list data from that particular call). .append worked in place of .extend.