Home > front end >  Unable to turn JSON to Dictionary in python from get request
Unable to turn JSON to Dictionary in python from get request

Time:02-03

I am trying to turn json I got from a GET request using an API into a dictionary that I can use. Here is what I did:

response = requests.get(API)
response_dict = json.loads(response.json())
print(response_dict)

I get the error:

raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict

CodePudding user response:

Carefully read the error. It suggests that response.json() already returns a dict. There is no need to call json.loads (which accepts a string) on it.

response = requests.get(API)
response_dict = response.json()

is all you need.

  •  Tags:  
  • Related