Python Taking API response and adding to JSON Key error

Question:

I have a script that takes an ID from a JSON file, adds it to a URL for an API request. The aim is to have a loop run through the 1000-ish ids and preduce one JSON file with all the information contained within.

The current code calls the first request and creates and populates the JSON file, but when run in a loop it throws a key error.

import json
import requests

fname = "NewdataTest.json"

def write_json(data, fname):
    fname = "NewdataTest.json"
    with open(fname, "w") as f:
        json.dump(data, f, indent = 4)
    with open (fname) as json_file:
        data = json.load(json_file)
        temp = data[0]
            #print(newData) 
        y = newData
        data.append(y)

# Read test.json to get tmdb IDs 
tmdb_ids = []
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()]
# Add IDs to API call URL
for tmdb_id in tmdb_ids:
    print("https://api.themoviedb.org/3/movie/" + str(tmdb_id) + "?api_key=****")
    # Send API Call
    response_API = requests.get("https://api.themoviedb.org/3/movie/" + str(tmdb_id) + "?api_key=****")
    # Check API Call Status
    print(response_API.status_code)
    write_json((response_API.json()), "NewdataTest.json")   
            

The error is in this line "temp = data[0]" I have tried printing the keys for data, nothing. At this point, I have no idea where I am with this as I have hacked it about it barely resembles anything like a cohesive piece of code. My aim was to make a simple function to get the data from the JSON, one to produce the API call URLs, and one to write the results to the new JSON.

Example of API reponse JSON:

{
    "adult": false,
    "backdrop_path": "/e1cC9muSRtAHVtF5GJtKAfATYIT.jpg",
    "belongs_to_collection": null,
    "budget": 0,
    "genres": [
        {
            "id": 10749,
            "name": "Romance"
        },
        {
            "id": 35,
            "name": "Comedy"
        }
    ],
    "homepage": "",
    "id": 1063242,
    "imdb_id": "tt24640474",
    "original_language": "fr",
    "original_title": "Disconnect: The Wedding Planner",
    "overview": "After falling victim to a scam, a desperate man races the clock as he attempts to plan a luxurious destination wedding for an important investor.",
    "popularity": 34.201,
    "poster_path": "/tGmCxGkVMOqig2TrbXAsE9dOVvX.jpg",
    "production_companies": [],
    "production_countries": [
        {
            "iso_3166_1": "KE",
            "name": "Kenya"
        },
        {
            "iso_3166_1": "NG",
            "name": "Nigeria"
        }
    ],
    "release_date": "2023-01-13",
    "revenue": 0,
    "runtime": 107,
    "spoken_languages": [
        {
            "english_name": "English",
            "iso_639_1": "en",
            "name": "English"
        },
        {
            "english_name": "Afrikaans",
            "iso_639_1": "af",
            "name": "Afrikaans"
        }
    ],
    "status": "Released",
    "tagline": "",
    "title": "Disconnect: The Wedding Planner",
    "video": false,
    "vote_average": 5.8,
    "vote_count": 3
}
Asked By: Thegasman2000

||

Answers:

You can store all results from the API calls into a list and then save this list in Json format into a file. For example:

#...

all_data = []

for tmdb_id in tmdb_ids:
    print("https://api.themoviedb.org/3/movie/" + str(tmdb_id) + "?api_key=****")
    # Send API Call
    response_API = requests.get("https://api.themoviedb.org/3/movie/" + str(tmdb_id) + "?api_key=****")

    # Check API Call Status
    print(response_API.status_code)

    if response_API.status_code == 200:
        # store the Json data in a list:
        all_data.append(response_API.json())

# write the list to file
with open('output.json', 'w') as f_out:
    json.dump(all_data, f_out, indent=4)

This will produce output.json with all responses in Json format.

Answered By: Andrej Kesely
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.