How to correct this error Json

0

I am collecting data from an Api that takes me back to the information in a Json dictionary to play in an appweb, so the server gives me this error, how can I do that to correct it?

Error giving:

Traceback (most recent call last):
  File "/home/dalmo/mysite/models/database.py", line 25, in <module>
    BancoDados.total()
  File "/home/dalmo/mysite/models/database.py", line 23, in total
    return BancoDados.Expert() + BancoDados.TS1()
  File "/home/dalmo/mysite/models/database.py", line 10, in Expert
    respo = json.loads(str(url.text))
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> 

My code:

import requests
import json

class BancoDados:

    def Expert():

        url = requests.get('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?'
            'apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')
        respo = json.loads(str(url.text))
        return(respo)



    def TS1():

        url = requests.get('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?'
            'apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=6a04ffe8-765a-4925-af26-d88029eeadba')
        respo = json.loads(str(url.text))
        return(respo)

    def total ():
        return BancoDados.Expert() + BancoDados.TS1()

BancoDados.total()

    
asked by anonymous 14.11.2018 / 21:55

1 answer

0

This error occurs when the first letter of the string is not valid in JSON.

It is very common when you think the site will return JSON, but it returns HTML for some reason, since HTMLs usually start with something like this:

<!DOCTYPE ...

Testing with json.loads() :

>>> json.loads('<!DOCTYPE')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

As you can see is exactly the same error you are getting. To check if this is the case, try to display the contents of the variable in some way before the error:

print(url.text)
    
14.11.2018 / 23:57