JSONDecodeError: Expecting value

455455 Member
edited December 2017 in Review my code

Hi. I'm using the Python code:

import requests
import json

app_id = '<###MyID>'
app_key = '<###MyKey>'

language = 'en'
word_id = 'Ace'

url = '###MyURL' + language + '/' + word_id.lower()

r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})

print("code {}\n".format(r.status_code))
print("text \n" + r.text)
print("json \n" + json.dumps(r.json()))

And the answer is:

runfile('C:/Users/Юрий/.spyder-py3/temp.py', wdir='C:/Users/Юрий/.spyder-py3')
code 403

text
Authentication failed
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/Юрий/.spyder-py3/temp.py', wdir='C:/Users/Юрий/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Юрий/.spyder-py3/temp.py", line 17, in
print("json \n" + json.dumps(r.json()))

File "C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)

File "C:\ProgramData\Anaconda3\lib\json__init__.py", line 354, in loads
return _default_decoder.decode(s)

File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

What am I doing wrong?

Comments

  • SimoneSimone Administrator admin

    Hi @455
    Thanks for your post, and sorry for the delayed response (for some reason there was a delay in your post coming through).
    But I'm on it now, let me get in touch with my colleagues and other API users to find someone who can review this for you - so apologies again for the delay, and bear with me! :)

  • Hi,

    I don't think there is anything wrong with your code but the API authentication keys you are sending are probably not quite right. Could you check your keys from the portal and try it again?

    The primary error is at the top - "code 403 - Authentication failed".

    The JSONDecode error is a secondary error because you are trying to decode the failing response. You can fix that by checking the status code before doing the JSON parse like this.

    import requests
    import json

    app_id = '<###MyID>'
    app_key = '<###MyKey>'

    language = 'en'
    word_id = 'Ace'

    url = '###MyURL Prefix' + '/api/v1/entries/' + language + '/' + word_id.lower()

    r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})

    print("code {}\n".format(r.status_code))
    print("text \n" + r.text)

    if r.status_code == 200:
    print("json \n" + json.dumps(r.json()))
    else:
    print("Failed: {} - {}".format(r.status_code, r.text))

    Please get in touch if you have any further problems.

    Thanks

    Justin

  • aryanaryan Member
    edited January 2018

    Hi,

    It might be that the url you are using is wrong. I could get your code working when I referred the following sample code:

    import requests
    import json
    
    app_id = '###'
    app_key = '###'
    
    language = 'en'
    word_id = 'Ace'
    
    url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.lower()
    
    r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
    
    print("code {}\n".format(r.status_code))
    print("text \n" + r.text)
    print("json \n" + json.dumps(r.json()))
    

    Please try this url and post here if there any any further issues.

  • SimoneSimone Administrator admin

    Hi @455 - just tagging you here so you can see the replies above.
    I hope these are useful, but do let us know if you still have any questions!

    @justinfoley and @aryan, many thanks indeed for your quick responses!

  • tmobbs175tmobbs175 Member
    edited January 2018

    Without the url being provided, it is a little difficult to determine whether this is a url issue or keys issue.

    I've just noticed that you have "<" and ">" characters in your id and keys. Maybe delete these characters and try again.

    app_id = '<###MyID>'
    app_key = '<###MyKey>'

    Please see example code below which is using the inflections url as an example with my own keys.

    language = 'en'
    word_id = 'Ace'
    url = 'https://od-api.oxforddictionaries.com:443/api/v1/inflections/' + language + '/' + word_id.lower()
    print(url) # Let's check that all looks good.
    r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
    print("code {}\n".format(r.status_code))
    print("text \n" + r.text)
    print("json \n" + json.dumps(r.json()))

    The output is:

    https://od-api.oxforddictionaries.com:443/api/v1/inflections/en/ace
    code 200

    text
    {
    "metadata": {
    "provider": "Oxford University Press"
    },
    ...
    ...
    ...
    ],
    "word": "ace"
    }
    ]
    }
    json
    {"metadata": {"provider": "Oxford University Press"}, "results": [{"id": "ace", "language": "en", "lexicalEntries": [{"grammaticalFeatures": [{"text": "Singular", "type": "Number"}], "inflectionOf": [{"id": "ace", "text": "ace"}, {"id": "ace", "text": "ACE"}], "language": "en", "lexicalCategory": "Noun", "text": "ace"}, {"grammaticalFeatures": [{"text": "Transitive", "type": "Subcategorization"}, {"text": "Present", "type": "Tense"}], "inflectionOf": [{"id": "ace", "text": "ace"}, {"id": "ace", "text": "ACE"}], "language": "en", "lexicalCategory": "Verb", "text": "ace"}, {"grammaticalFeatures": [{"text": "Singular", "type": "Number"}], "inflectionOf": [{"id": "ace", "text": "ace"}, {"id": "ace", "text": "ACE"}], "language": "en", "lexicalCategory": "Noun", "text": "ACE"}, {"grammaticalFeatures": [{"text": "Positive", "type": "Degree"}], "inflectionOf": [{"id": "ace", "text": "ace"}, {"id": "ace", "text": "ACE"}], "language": "en", "lexicalCategory": "Adjective", "text": "ace"}], "word": "ace"}]}

    Hope this helps.

  • tmobbs175tmobbs175 Member

    Do you have "<" and ">" characters in your id and keys? If so, please delete and try again.

    app_id = '<###MyID>' ---------->> should be app_id = '###MyID'
    app_key = '<###MyKey>' ---------->> should be app_id = '###MyKey'

    if not, then try the url in following format.

    url = 'https://od-api.oxforddictionaries.com:443/api/v1/inflections/' + language + '/' + word_id.lower()

    Hope this helps.

  • SumoBeeSumoBee Member
    edited January 2018

    Hi, @455.

    I use python for the API as well. It's been a while since I set up the code, but I see that I had some issues as well as I started using the same code you did and now have commented it out and modified.

    I started with json.dumps(r.json()) as you did here:

       print("json \n" + json.dumps(r.json()))
    

    The change that worked for me:

       listing = r.json()
       results = listing['results']
    

    So perhaps you could change to:

       print("json \n" + r.json()['results']  
    

    Edited the above line to include parentheses before ['results']

  • The first line of the output (r.text) says "Authentication failed". You won't get anything useful in r.json until your authentication works.

  • serenityserenity Member
    edited January 2018

    As you faced authentification problem, first of all you should check API keys. If they have status - live? You should also be shure you didn't unintentionally put unnecessary spaces or braces in your app_key and app_id strings.

  • SimoneSimone Administrator admin

    Hi, @455
    Please see a couple of additional comments above :)

  • Hello @455,
    According to the error trace you showed, it seems like your app id and the secret key may be invalid. and JSONDecodeError is showing because the response text isn't a JSON array or JSON Object.

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @sprasad142,

    That would tally with the reported error message, I think. This is a tricky area in that we obviously wouldn't recommend for anyone to post their app_id/app_key publicly but it does mean that we can have difficulty assessing issues where these are being used incorrectly.

    If anyone is reading this thread with similar issues, I would recommend looking at the app_id and app_key displayed on the documentation page (note that they won't work on the live API!) and if yours doesn't look similar then you may have a problem.

Sign In or Register to comment.