How to fix this Json error

Asked

Viewed 1,142 times

0

I’m collecting data from an Api that retrieves the information in a Json dictionary to play on an appweb so the server gives me this error how can I fix it?

Mistake you’re making:

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()

inserir a descrição da imagem aqui

  • 1

    You should put the complete error with traceback, in text format, not image. Edith the question and add the error in text. Also put the relevant part of your code.

1 answer

1

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

It’s very common when you think the site will return JSON, but it returns HTML for some reason, because 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 it is exactly the same error you are getting. To check if this is the case, try to display the contents of the variable somehow, before the error:

print(url.text)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.