Reading a json-file from an API, getting "AttributeError: 'Response' object has no attribute 'data' "error

Title says it all. Im trying to read a json file im accessing via an API. But when I try, I get the error:

'AttributeError: 'Response' object has no attribute 'data'

I don't understand why, as most (not all) of my fellow classmates do not get this error.

The code I'm trying to run:

[1] import folium
[2] import geopandas as gpd
[3] import pandas as pd
[4] import numpy as np
[5] import urllib3
[6] import requests
[7] import json
[8] import os

[9] dmi_api = 'f8654190-89bb-43d2-9e08-416346b79e7b'
[10] url = 'https://dmigw.govcloud.dk/v2/metObs/collections/observation/itemsstationId=06031parameterId=temp_drydatetime=2021-01-01T00:00:00Z/2021-03-01T00:00:00Zapi-key='+str(dmi_api)
[11] r = requests.get(url)
[12] print(r)
[13] print(url)

[14] data = json.loads(r.data.decode('utf-8')) #This is where the error is referring to (which leads back to line 11)
[15] data

What I don't understand is why the json.loads isnt working, as the link is obviously full of data.

I'm coding in Python 3.2 using the Jupyter Notebook accessed from Anaconda, if that's any help.

Topic jupyter anaconda python

Category Data Science


This is not a data science question and you should try programming stack-exchange instead. But this is happening because of an API mismatch in Python, specifically the type of the GET method of the requests object. It's likely the code snippet you got is from a version that is not python 3.2 or a requests module that is not up to date. I would try to install a compatible version of the requests module from PyPI or make your python version compatible with the snippet.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.