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
2
Answers
You can use
json.loads
to read the file’s content and then iterate to find those id’s:Result:
You can make it more sophisticated if you need a specific
tmdb_id
from a specific "movie" by adding some if statements to thefor
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:for reading json:
for tmbd_id: