Reading a JSON file in python, reading a value and assigning it to a variable


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

Asked By: Thegasman2000



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():



[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.


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()]
Answered By: no_hex

for reading json:

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

for tmbd_id:

for i in text:
Answered By: Vishal Nagare
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.