0

So I ran into an issue while importing API data into my code. Any help is much appreciated.

from urllib2 import Request, urlopen, URLError
import json, requests

data = requests.get('https://masari.superpools.net/api/live_stats?update=1522693430318').json()
data_parsed = json.loads(open(data,"r").read())
print data_parsed

I'm still quite new to python, and I ran into this error:

>C:\Users\bot2>python C:\Users\bot2\Desktop\Python_Code\helloworld.py
Traceback (most recent call last):
  File "C:\Users\bot2\Desktop\Python_Code\helloworld.py", line 5, in <module>
    data_parsed = json.loads(open(data,"r").read())
TypeError: coercing to Unicode: need string or buffer, dict found
3
  • You don't need the open statement in json.loads here; that's just for file reading. Just use json.loads(data). See docs Commented Apr 2, 2018 at 20:29
  • @patrick data is already a dictionary as .json() was added to request Commented Apr 2, 2018 at 20:30
  • @GüntherJena I stand corrected. Commented Apr 2, 2018 at 20:31

1 Answer 1

0

data is already received as a json object (which is a dict in this case). Just do the following:

data = requests.get('https://masari.superpools.net/api/live_stats?update=1522693430318').json()
print data

Use data['network'] for example to access nested dictionaries.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I have an additional question, how would I pull specific data from the API? I would like to pull all data from the category "network" in the API.

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.