1

I've been trying to use an API with the following response when I type in the url:

{
    "resource": "playerdashptshotlog",
    "parameters": {
        "LeagueID": "00",
        "Season": "2014-15",
        "SeasonType": "Regular Season",
        "PlayerID": 202066,
        "TeamID": 0,
        "Outcome": null,
        "Location": null,
        "Month": 0,
        "SeasonSegment": null,
        "DateFrom": null,
        "DateTo": null,
        "OpponentTeamID": 0,
        "VsConference": null,
        "VsDivision": null,
        "GameSegment": null,
        "Period": 0,
        "LastNGames": 0
    },
    "resultSets": [

My code is as follows:

import json, requests
github_url = 'http:dsds
parsed_input = json.loads(github_url)
print (parameters.keys())
print (parameters['LeagueID']['Season'])

I get an error when I use Python34 saying:

Traceback (most recent call last): File "C:\Python34\Scripts\NBA API-JSON.py", line 27, in parsed_input = json.loads(github_url) File "C:\Python34\lib\json__init__.py", line 318, in loads return _default_decoder.decode(s) File "C:\Python34\lib\json\decoder.py", line 343, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None ValueError: Expecting value: line 1 column 1 (char 0)

When I run it on Python27 I get this error:

Traceback (most recent call last): File "C:\Python27\Scripts\NBA API-JSON.py", line 27, in parsed_input = json.loads(github_url) File "C:\Python27\lib\json__init__.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

I'm trying to figure out what I'm doing wrong. I tried using an example answer I found to a question found at:

Parsing Multidimensional JSON Array

2
  • Did you not copy all of your response, because what you pasted isn't valid JSON, it ends with an open [ Commented Sep 22, 2015 at 23:45
  • It's about 60 pages worth Commented Sep 22, 2015 at 23:48

1 Answer 1

2

It looks like you forgot to fetch the data. Try:

github_url = 'http://whatever'
r = requests.get(github_url)
if r.status_code == 200:
    parsed_input = json.loads(r.text)

Requests can also parse the JSON for you:

parsed_input = r.json()
Sign up to request clarification or add additional context in comments.

3 Comments

And how can I just print certain fields, eg: TeamID, PlayerID etc? So that I may manipulate any of these values later on like be in a tabular format.
@RossJohnson I don't think that's relevant to your question. I believe that this answer answers your question. If you think it does then it would be best to upvote/accept it. I'd suggest creating another question for this with more details.
json.loads() returns a regular python dict or list, depending what the input JSON is. In your case do parsed_input['parameters']['TeamID'] and so on.

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.