1

I am trying to use Python and APIs to build an SQL table with the names of the first 100 Pokemon in the Poke API. Here's my code

import psycopg2, json, requests, hidden

# Load secrets
secrets = hidden.secrets()

conn = psycopg2.connect(host=secrets['host'],
        port=secrets['port'],
        database=secrets['database'],
        ...,
        connect_timeout=3)

cur = conn.cursor()

defaulturl = 'https://pokeapi.co/api/v2/pokemon?limit=100&offset=0'


sql = '''
CREATE TABLE IF NOT EXISTS pokeapi
(id SERIAL, body JSONB); 
'''

cur.execute(sql)

response = requests.get(defaulturl)
js = response.json() 


results = js['results'] 



for x in range(len(results)):
    body = requests.get(results[x]['url'])
    js_body = json.dumps(body) 
    sql = f"INSERT INTO pokeapi (body) VALUES ('{js_body}'::JSONB)";
    cur.execute(sql, (defaulturl))

print('Closing database connection...')
conn.commit()
cur.close() 

And the error is coming up for this line

---> 35 js_body = json.dumps(body)

I'm not sure what's causing the error.

1 Answer 1

2

requests.get() returns the entire response, not just the body text.

If you want the body text, use the text attribute of the response object:

response = requests.get(results[x]['url'])
js_body = json.dumps(response.text)
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thanks for the input. I want to get just the names of the first 100 in the column body, this gives the whole JSON line I think.
@hulio_entredas So, iterate over the entries and stop processing once you reach 100. What is the difficulty?
Oh, okay. When I ran a test it didn't look like the SQL table was filling the body column with Pokemon names with this code.
@hulio_entredas My answer only addressed the json error. I have no idea if it is filling the SQL table properly.
Right, makes sense. Sorry about that. Thanks for your help! I just had to change the lines to js_body = body.text and cur.execute(sql, (js_body,)) in the for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.