How to Fix: AttributeError: ‘Response’ object has no attribute ‘json’

3D illustration of a robot looking for a label on a box instead of pressing the button, representing the missing parentheses on the json method.

This AttributeError Response object issue is one of the most common typos when working with APIs and the requests library.

It means: “You typed response.json but you meant response.json().

The Cause

In Python, there’s a huge difference between an attribute (a piece of data, like a variable) and a method (an action, like a function).

  • response.status_code is an attribute. It’s just a number (like 200).
  • response.text is an attribute. It’s just a string of text.
  • response.json() is a method. It’s an action that tells Python to read the response.text and parse it into a Python dictionary.

Problem Code:

import requests
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")

# This is a method, not an attribute!
data = response.json
# CRASH! AttributeError: 'Response' object has no attribute 'json'

The Fix: Add Parentheses

You need to call the method by adding () at the end.

import requests
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")

# THE FIX:
data = response.json() # Call the method

# Now it's a dictionary!
print(data['bpi']['USD']['rate'])

If you ever see an AttributeError, your first check should be: “Did I forget the ()?”

Similar Posts

Leave a Reply