PYTHON: INTERACTING WITH API 1. I am trying to request data from an API url(Note: Im using a public API for this example

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

PYTHON: INTERACTING WITH API 1. I am trying to request data from an API url(Note: Im using a public API for this example

Post by answerhappygod »

PYTHON: INTERACTING WITH API
1. I am trying to request data from an API url(Note: Im using a
public API for this example but the one for my actual project
requires an API key)
2. I am then trying to extract specific keys from that data and
print their values.
3. Problem: I cannot extract these keys from the data set
requested from the API.
Code:
next_API =
requests.get("https://www.boredapi.com/api/activity")
data2 = next_API.text
stuff = json.loads(data2)
parse = stuff['activity']['price'] #From here I would
like to extract the 'price' and 'activity' keys from the data set
and print their values
print(parse)
Output:
TypeError: string indices must be integers
How can I fix this?
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply