0

I have a json file which looks like

{
        "0": {
            "id": 1700388,
            "title": "Disconnect: The Wedding Planner",
            "year": 2023,
            "imdb_id": "tt24640474",
            "tmdb_id": 1063242,
            "tmdb_type": "movie",
            "type": "movie"
        },
        "1": {
            "id": 1631017,
            "title": "The Pale Blue Eye",
            "year": 2022,
            "imdb_id": "tt14138650",
            "tmdb_id": 800815,
            "tmdb_type": "movie",
            "type": "movie"
        },
        "2": {
            "id": 1597915,
            "title": "The Man from Toronto",
            "year": 2022,
            "imdb_id": "tt11671006",
            "tmdb_id": 667739,
            "tmdb_type": "movie",
            "type": "movie"
        },

I am trying to read this and extract the "tmbd_id" to store as a variable. I then intend to inject this into a url for an api get request.

The next step is to add the response parameters to the json file. Then adding it all into a loop. There are 1000 entries.

I have been trying to use other answers but I suspect the nature of my json is causing the issue with the integer name causing issues. It has been giving me this error

for i in data['0']: TypeError: list indices must be integers or slices, not str

Thegasman2000
  • 95
  • 3
  • 10

2 Answers2

1

You can use json.loads to read the file's content and then iterate to find those id's:

import json

tmdb_ids = []

with open('test.json', 'r') as json_fp:
    imdb_info = json.load(json_fp)[0]

for dict_index, inner_dict in imdb_info.items():
    tmdb_ids.append(inner_dict['tmdb_id'])

print(tmdb_ids)

Result:

[1063242, 800815, 667739]

You can make it more sophisticated if you need a specific tmdb_id from a specific "movie" by adding some if statements to the for loop, choosing movies you want.


Edit

I've noticed this is an array of dictionaries, thus we can iterate over each "movie chunk" and extract the tmdb_id from each one:


with open('test.json', 'r') as json_fp:
    imdb_info = json.load(json_fp)

tmdb_ids = [movie_info['tmdb_id'] for movies_chunk in imdb_info for movie_index, movie_info in movies_chunk.items()]
CodeCop
  • 1
  • 2
  • 15
  • 37
  • for dict_index, inner_dict in tmdb_info.items(): AttributeError: 'list' object has no attribute 'items' This is the error running the above code.. – Thegasman2000 Jan 20 '23 at 16:54
  • @Thegasman2000 Sampling from the data you've given it works - but I see it isn't the full data.. could you provide it? (the full JSON and code) + I see you changed some names in my code (you're getting an error `tmdb_info.items()` I don't know what you also changed, maybe you define another variable with the same name of type list afterwards) – CodeCop Jan 20 '23 at 16:56
  • Sure I'm not sure whats up with the json, but as I collated it I don't doubt something is amiss. https://wetransfer.com/downloads/b6d08029de942eeedac7a6b5c41de90220230120165810/3f8249 Thanks – Thegasman2000 Jan 20 '23 at 17:00
  • And sorry just. saw your edit. I changed the imdb to tmdb. Just tried fresh again, incase I missed something and same error. Thanks – Thegasman2000 Jan 20 '23 at 17:03
  • @Thegasman2000 That is because the JSON starts in brackets. I've updated my code – CodeCop Jan 20 '23 at 17:04
  • Ah ha thank you! Yeah, the data came out the api response wonky I think. This is now giving me a list of all the ids, can I read just one at a time to add to a loop. As i will inject this into a different api url. Thanks again – Thegasman2000 Jan 20 '23 at 17:06
  • @Thegasman2000 Wait, now that I see - your json contains empty dictionaries, which is not giving you the full list of `tmdb_id` - I will quickly find an answer, but if my solution is good enough tell me :) – CodeCop Jan 20 '23 at 17:07
  • This is working great thanks. I am able to set the first value as a variable which I can insert into the api call. Just need to figure the loop bit out but thanks so much. :) – Thegasman2000 Jan 20 '23 at 17:37
  • @Thegasman2000 If you're getting stuck with it - open another question :) we will gladly help. You're welcome and thanks for your time! – CodeCop Jan 20 '23 at 17:39
0

for reading json:

import json
with open("file.json","r") as file:
    text = json.load(file)

for tmbd_id:

for i in text:
    print(text[i]["tmdb_id"])